Before moving on to some real action let’s get familiar with some of the facts on Activity:
- An activity is a portion of work that is completed with the help of other resources.
- Activities are the basic building blocks of workflows.
- A workflow can be created by using a single activity or a group of activities. SharePoint 2010 enables both out-of-the-box and custom activities.
- Out-of-the-box activities can be used directly in Microsoft SharePoint Designer 2010; custom activities require some coding.
Now let’s see some facts on Custom Code Activity With a wide selection of out-of-the-box activities, why are custom activities needed at all? This question is especially relevant when you consider that it is also fairly easy to execute code in an existing workflow by adding a CodeActivity and then writing the code in the ExecuteCode event handler. However, custom activities are especially helpful because they wrap custom code in a reusable component. Further, as the following examples show, some essential actions require more functionality than out-of-the-box activities can provide.
- Looping: Out-of-the-box activities cannot provide direct looping support through collections, such as the lists in a site or the items in a list.
- Accessing outside content: With out-of-the-box activities, it is not possible to access SharePoint content in other sites or site collections or to access anything at the farm or web application level.
- Calling across the network: Standard actions do not support calling a remote web service from a SharePoint Designer 2010 workflow.
- Running with elevated permissions: Out-of-the-box activities run under the current user’s identity and permissions. Although Impersonation steps can help mitigate this, workflow actions are still restricted from running under a more privileged user’s identity and elevated permissions.
- Obtaining control at the workflow level: By its nature, SharePoint Designer 2010 insulates the workflow builder from the details of the underlying SharePoint workflow architecture.
Enough of the theory for now. Now it’s time to see some action. Steps to Implement Custom Activity:
- Add a new project using class library project template
- Add new workflow activity as shown below
- Add code to the activity as shown below
- Dependency properties indicate that the workflow depends on this value. All values that are used as inputs or outputs for the custom workflow activity should be marked as dependency properties. Following are the available dependency properties in our code:
- SiteUrlProperty: This property maps with SiteUrl Browsable Property.
- ListINameProperty: This property maps with ListName Browsable Property.
- ListTemplateProperty: This property maps with ListTemplate Browsable Property.
- Browsable Properties are the simple properties decorated with a number of Attributes. Browsable Properties must be mapped with corresponding Dependency Properties. Following are the available Browsable Properties in our code:
- Execute Method: The Execute method must complete by returning a value from the ActivityExecutionStatus enumeration. Enumeration of activity status values corresponds to the life cycle of an activity in a running workflow instance. Once execution of the code is complete, you must call the enumeration Closed for the activity. The following table shows different enumerations for custom activities:
- CreateList Method: This is just a simple helper function which creates the SharePoint List using server side object model.
And that’s the end of our analysis. Now lets continue with the remaining steps
- Add Strong Name Key to the Project by going to the Project Properties > Signing Tab as shown below:
- Build the solution
Steps to Implement Custom Activity Deployment Project:
- Add Empty SharePoint Project as shown below:
- Add the reference of CustomWorkflowActivties.dll in this solution
- Add SharePoint Mapped Folder TEMPLATE\1033\Workflow
- Add xml file with the name CreateListWorkflowActivity.Actions, make sure this file must have the .Actions extension otherwise SharePoint Runtime would not get it recognized as an workflow action.
- Add the configuration entries to the CreateListWorkflowActivity.Actions file as shown below:
- Add AuthorizedType entry to Web.config file of Web Application for this custom workflow action, else it won’t get displayed in SharePoint Designer > Workflows > Action ribbon menu
Note: To make a full-trust workflow activity available to declarative workflows, add an authorizedType entry to the Web.config file for the content web application. This gives your custom activity the same status as the built-in workflow activities that are included with SharePoint 2010. The following code example shows the format of an authorizedType entry.
- Build the solution
- Deploy the solution
And with this we are done with our development work, now it is the time to test our efforts. Steps to test Custom Activity:
- Launch SharePoint Designer
- Connect to SharePoint Site
- Click on left navigation link “Workflows”
- Click on Site Workflows in the ribbon. Enter Name and Description for the workflow.
- Choose Action Menu > Custom Workflow Activities > Create SharePoint List Action
- Configure Create SharePoint List Action by providing following information
- List Name
- Site Url
- List Template
- The activity will look like as below after configuration
- Save the Workflow by selecting Save option in the ribbon
- Check for the errors in Workflow before publishing it by selecting Check for Errors option in the ribbon
- Publish the Workflow by selecting Publish option in the ribbon
- Navigate to the SharePoint Site
- Click Site Settings > View All Site Content
- Click on Site Workflows Link
- Start the workflow
- On completion of Workflow, go to View Site Content > Lists, and you should be able to see the new list of type Announcement