Share via


How to: Create a Sandboxed Workflow Action

Overview

Microsoft® SharePoint® 2010 enables you to create and deploy custom workflow actions as sandboxed solutions. You can then consume these workflow actions within declarative workflows in SharePoint Designer. This how-to topic describes how to create and deploy a sandboxed workflow action.

For a practical example of the deployment of a sandboxed workflow action, see the workflow activities reference implementation.

Summary of Steps

This how-to topic includes the following steps:

  • Step 1: Create the SharePoint Project. In this step, you create a Visual Studio 2010 project that you can use to deploy and test your sandbox proxy.
  • Step 2: Create the Workflow Action Class. In this step, you create a class that contains your sandboxed logic.
  • Step 3: Create the Workflow Action Definition. In this step, you create a feature manifest file. This contains the markup that references your class and defines your workflow action.

Step 1: Create the SharePoint Project

This procedure creates a SharePoint project in the Microsoft Visual Studio® 2010 development system. You can use this project to build and deploy your sandboxed workflow action.

To create a project

  1. Start Visual Studio 2010 and create a new Empty SharePoint Project, as shown in the following illustration. Name the project SimpleAction.

    Ff798499.0498a75c-3674-48c4-8feb-2a4a872c61ad(en-us,PandP.10).png

  2. In the SharePoint Customization Wizard, specify a valid local site for debugging, select Deploy as a sandboxed solution, and then click Finish.

    Ff798499.61b27689-72c9-44b8-89c3-000bf9e422e4(en-us,PandP.10).png

Step 2: Create the Workflow Action

This procedure creates a sandboxed workflow action class. It contains a method that defines the workflow action logic, and it shows you how to return values to the workflow.

To create a sandboxed workflow action class

  1. Add a new class named SandboxActivityLog to the project.

  2. Add the following using statements to your class.

    using System.Collections;
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.UserCode;
    using Microsoft.SharePoint.Workflow;
    
  3. Add the public access modifier to your class.

  4. Within the class, add a public method that accepts an argument of type SPUserCodeWorkflowContext and returns a value of type Hashtable. This method defines the workflow action.

    public class SandboxActivityLog
    {
      public Hashtable Log(SPUserCodeWorkflowContext context) { }
    }
    
  5. Within your method, implement your action logic. You can return values from your method by adding key/value pairs to a Hashtable object.

    public Hashtable Log(SPUserCodeWorkflowContext context)
    {
      Hashtable results = new Hashtable();
      results["Except"] = string.Empty;
    
      try
      {
        using (SPSite site = new SPSite(context.CurrentWebUrl))
        {
          using (SPWeb web = site.OpenWeb())
          {
            SPWorkflow.CreateHistoryEvent(web, context.WorkflowInstanceId, 0,
                web.CurrentUser, TimeSpan.Zero, "Information",
          "Event from sandboxed activity", string.Empty);
          }
        }
      }  
      catch (Exception ex)
      {
        results["Except"] = ex.ToString();
      }
    
      results["Status"] = "Success";
      return (results);
    }
    

    Note

    Notice the use of the Except key to return an exception to the workflow.

Step 3: Create the Workflow Action Definition

This procedure creates a workflow action definition in a feature manifest file. This markup tells the SharePoint workflow engine how to interact with your workflow action.

To create an action definition for a sandboxed workflow action

  1. In Solution Explorer, right-click the project node, point to Add, and then click New Item.

  2. In the Add New Item dialog box, in the Installed Templates pane, expand SharePoint, and then click 2010.

  3. Click Empty Element, type a name for the element in the Name text box, and then click Add. This example uses the name LogDefinition for the element.

    Ff798499.c9e58967-3503-4229-ae0a-3cbf819ad937(en-us,PandP.10).png

  4. Expand the LogDefinition node and open the feature manifest file (Elements.xml).

  5. Add a WorkflowActions element to the feature manifest.

  6. Within the WorkflowActions element, add an Action element. This should do the following:

    1. Provide a friendly name for your workflow action.
    2. Specify that the action runs in the sandbox.
    3. Identify the assembly name and class name of your action.
    4. Identify the method that provides the action's functionality.
    <Elements xmlns="https://schemas.microsoft.com/sharepoint/">
      <WorkflowActions>
        <Action Name="Log Testing" 
                SandboxedFunction="true" 
                Assembly="$SharePoint.Project.AssemblyFullName$" 
                ClassName="SimpleAction.SandboxActivityLog" 
                FunctionName="Log" 
                AppliesTo="all" 
                Category="patterns and practices sandbox">
        </Action>    
      </WorkflowActions>
    </Elements>
    
  7. Within the Action element, add a RuleDesigner element. This specifies the sentence that your workflow action will display in SharePoint Designer. It also binds the Except argument returns by your action to a variable named Exception.

    <RuleDesigner Sentence="Log Activity (Exception to %1)">
      <FieldBind Field="Except" 
                 Text="Exception" Id="1" 
                 DesignerType="ParameterNames" />
    </RuleDesigner>
    
  8. Within the Action element, add a Parameters element. This should define the arguments passed to your workflow action method and the types returned by your workflow action method.

    <Parameters>
      <Parameter Name="__Context" 
                 Type="Microsoft.SharePoint.WorkflowActions.WorkflowContext, 
                       Microsoft.SharePoint.WorkflowActions" 
                       Direction="In" 
                       DesignerType="Hide"/>
      <Parameter Name="Except" 
                 Type="System.String, mscorlib" 
                 Direction="Out" 
                 DesignerType="ParameterNames" 
                 Description="Exception encountered"/>
    </Parameters>
    

    Note

    The context argument must be preceded by a double underscore, as shown (__Context).

  9. When you added the empty element to your project, Visual Studio created a feature named Feature 1. In Solution Explorer, right-click Feature 1, and then click Rename. This example uses the name SimpleActionFeature for the feature.

  10. Double-click SimpleActionFeature to open the feature designer, and then change the scope of the feature to Site.

  11. Press F5 to deploy and test your sandboxed workflow action.

  12. To verify that your sandboxed workflow action deployed successfully, open SharePoint Designer and create a new workflow. You should find that the Log Testing action has been added to the list of available actions.

    Ff798499.fa98095e-d186-4bf6-8e89-32a2eab657e7(en-us,PandP.10).png