Tuesday, May 11, 2010

How to copy file using copy.asmx sharepoint web service

To copy files first we need to add a web service in Visual Studio 2008. The link to add should be

http://sharepoint/sites/yoursite/_vti_bin/Copy.asmx

Just append the /_vti_bin/Copy.asmx behind the sharepoint sites that we need to copy file.

The next step is to configure NTLM authentication for sharepoint. (I assume you use the default NTLM authentication instead of the Kerberos. You will have to change the WCF configuration as follow in binding section

<binding name="CopySoap" closeTimeout="00:01:00" openTimeout="00:01:00"
receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false"
bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferSize="67108864" maxBufferPoolSize="67108864" maxReceivedMessageSize="67108864"
messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="10000000" maxStringContentLength="10000000"
maxArrayLength="67108864" maxBytesPerRead="65536" maxNameTableCharCount="100000" />
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Ntlm" proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>

Take note of the clientCredentialType="Ntlm" setting at transport credential.

The following code will enable the user to copy file from 1 location to another

CopyIntoItemsLocal function did the copying of the file. If you want to copy the file from sharepointserver 1 to sharepointserver2 this method cannot be used. You have to use CopyIntoItems method instead. For my case, I am copying file within the same sharepoint server. So the code as follow

Imports System.Net
Imports System.IO
Imports System
Module Module1

Sub Main()

Dim copyService As CopyWebService.CopySoapClient = New CopyWebService.CopySoapClient()
copyService.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation
copyService.ClientCredentials.Windows.ClientCredential.Domain = "yourdomain"
copyService.ClientCredentials.Windows.ClientCredential.UserName = "adminuser"
copyService.ClientCredentials.Windows.ClientCredential.Password = "yourpassword"

Dim myCopyResult1 As New CopyWebService.CopyResult
Dim myCopyResultArray As CopyWebService.CopyResult() = {myCopyResult1}

Try

Dim copySource As String = "http://sharepoint/sites/mysite/Report/Test.xlsx"
Dim copyDest As String() = {"http://sharepoint/sites/mysite/Report/TestCopyFile.xlsx"}
Dim myUint As System.UInt32 = copyService.CopyIntoItemsLocal(myCopyResultArray, copySource, copyDest)
If myUint = 0 Then
Dim idx As Integer = 0
Dim myCopyResult As CopyWebService.CopyResult
For Each myCopyResult In myCopyResultArray
Dim opString As String = (idx + 1).ToString()
If myCopyResultArray(idx).ErrorMessage Is Nothing Then
Console.WriteLine("Copy Operation Ok")
Else
Console.WriteLine("Copy Operation fail")
End If
idx += 1
Next myCopyResult
End If
Catch exc As Exception
Dim idx As Integer = 0
Dim myCopyResult As CopyWebService.CopyResult
For Each myCopyResult In myCopyResultArray
idx += 1
If myCopyResult.DestinationUrl Is Nothing Then
Dim idxString As String = idx.ToString()
Console.WriteLine("Copy operation failed." + _
myCopyResult.ErrorMessage + myCopyResult.ErrorCode + _
"Description: " + exc.Message, "Exception")
End If
Next myCopyResult
End Try

End Sub

End Module

No comments: