Creating Sandboxed Actions

In the Workflow Activities Reference Implementation (Workflow Activities RI), the CopyLibrary class is a sandboxed workflow action class that copies a document library from the current site to a target site in the same site collection. When you create a sandboxed workflow action class, the class must include a public method that meets the following criteria:

  • The method must accept an argument of type SPUserCodeWorkflowContext, plus any arguments required by your activity logic.
  • The method must return an object of type Hashtable.

The CopyLibrary class provides a method named CopyLibraryActivity that meets the criteria in the following code example.

public Hashtable CopyLibraryActivity(SPUserCodeWorkflowContext context, 
  string libraryName, string targetSiteUrl)
  return (CopyLibrary(context.WebUrl, libraryName, targetSiteUrl));

This method calls a second method named CopyLibrary that contains the workflow action logic. This performs the actual work and is used because it is easier to test than the signature required by SharePoint Designer. This method performs the actions shown in the following illustration.

The Copy Library workflow action



CopyLibraryActivity calls CopyLibrary instead of having overloads because SharePoint is not capable of determining which overload to use. Methods that are referenced for sandboxed workflow actions cannot be overloaded.

Notice how the method uses the hash table to return results. The method adds two entries to the hash table: a string value that indicates success or failure and an integer value that indicates the number of files copied from the source library to the target library.

Hashtable results = new Hashtable();
int copiedFiles = CopyFolder(sourceLib.RootFolder, targetLib.RootFolder, true);
results["status"] = "success";
results["copiedFiles"] = copiedFiles;
return results;

If the source list does not exist, the method returns a status value of failure and a copied files count of zero.

results["status"] = "failure";
results["copiedFiles"] = 0;
return (results);

For more information about returning values from a sandboxed workflow action, see How to: Create a Sandboxed Workflow Action.