CopyMove SOAP Web Service

Top  Previous  Next

Remote clients can also leverage CopyMove through its integrated WCF SOAP Web Service that wraps the .NET API. The service is available at URLs constructed from the following pattern:

<protocol>://<Web application>/<Context Site>/_vti_bin/CopyMove.svc

where the protocol can be HTTP or HTTPS depending on the Web application setup. For example:

http://server/_vti_bin/CopyMove.svc or http://server/sites/site/_vti_bin/CopyMove.svc

The service is sensitive to the SharePoint site context and it is therefore important to always connect with the proper SharePoint site url address. From Visual Studio it is easy to connect to the service and build the required client classes. Just note that you need to add /mex to the service URL as shown in the screen shot below.

VisualStudio_AddServiceReference

Once Visual Studio has successfully connected to the service, click OK to generate the client code that can be used to invoke the service. To include support for asynchronous invocation, first click the Advanced button and check the Generate asynchronous operations check box. Visual Studio will in turn generate a class named CopyMoveServiceClient with the following methods:

CopyMoveResult Copy(CopyMoveItemTransaction transaction);

 

IAsyncResult BeginCopy(CopyMoveItemTransaction transaction, AsyncCallback callback, object asyncState);

 

CopyMoveResult EndCopy(IAsyncResult result);

 

CopyMoveResult Move(CopyMoveItemTransaction transaction);

 

IAsyncResult BeginMove(CopyMoveItemTransaction transaction, AsyncCallback callback, object asyncState);

 

CopyMoveResult EndMove(IAsyncResult result);

 

CopyMoveStatus GetStatus(string transactionId);

 

string GetVersion();

See http://msdn.microsoft.com/en-us/library/ms734691.aspx for more information about accessing WCF services. Connecting to the CopyMove WCF SOAP service in code goes as in the following example with NTLM authentication. For other types of supported authentication schemes, please refer to the Microsoft WCF documentation on MSDN.

//

// Create proxy that connects to the service and authenticates with current user.

//

var binding = new BasicHttpBinding();

binding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;

binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Ntlm;

var endpoint = new EndpointAddress("http://server/sites/site/_vti_bin/CopyMove.svc");

CopyMove.CopyMoveServiceClient proxy = new CopyMove.CopyMoveServiceClient(binding, endpoint);

proxy.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;

 

//

// Get the CopyMove version number. This service call will fail if a

// connection to the WCF Service cannot be established.

//

string version = proxy.GetVersion();

From here the code is virtually identical to that of the .NET API. The following example moves a folder and two documents from one document library to another document library within the same SharePoint farm.

// Prepare new CopyMove item transaction

var transaction = new CopyMoveItemTransaction();

transaction.FileExistsAction = CopyMoveFileExistsAction.Overwrite;

transaction.IncludeTimestamps = true;

transaction.IncludeUserInfo = true;

transaction.IncludeVersions = true;

transaction.IncludeSecurity = false;

transaction.HaltOnWarning = false;

         

// The absolute URL of the source folder or source list

transaction.SourceUrl = "http://server/sites/site1/doclib/folder";

 

// The absolute URL of the target folder or target list

transaction.TargetUrl = "http://server/sites/site2/doclib/folder";

 

// The source items to copy. Each item can be specified by its

// absolute URL, server relative URL, filename, list item GUID or list item ID.

// In this example, we just specify the filenames of a folder and two documents

// in the source folder specified above.

transaction.Items = new string[]

{

  "Test Folder", "Test Document.doc", "Test Document.pdf"

};

 

// Optional: Exclude one or more list columns

transaction.ExcludeProperties = new string[] { "FieldName1", "FieldName2" };

 

// Run the specified move transaction.

CopyMoveResult result = proxy.Move(transaction);

 

if (result.ErrorCode == 0)

{

  // Success - the transaction completed without errors or warnings

} else if (result.ErrorCode == 1)

{

  // Warning - the transaction completed with warnings

} else if (result.ErrorCode == 2)

{

  // Error - the transaction was aborted with errors

}

The sample code shown above can also be found in the Form1.cs file of the Visual Studio project named WCF Sample. It is included in the CopyMove product download.