Saturday 10 October 2015

Custom Workflow Activity

Create Custom Workflow Activity in SharePoint 2010

This blog demonstrates how to build the custom workflow activity in Visual Studio 2010 and use this workflow activity in SharePoint Designer 2010 as a custom action.
First you need to create the “Empty SharePoint Project” in Visual Studio 2010.


Please provide the URL of the SharePoint Site where you want to deploy the workflow. Also 
make sure that you select the “Deploy Solution as a farm solution”. Click next after that.


Before you begin you need to add the following reference to your project.
§  Microsoft.SharePoint.dll
§  Microsoft.Office.Workflow.Actions.dll
§  System.Workflow.ComponentModel.dll
There are two major steps to create a Custom action:
1.    Add a .class File
2.    Add WSS.actions file

Add a .class file
Add the new class to the solution and make sure that class is public otherwise you will get the “protection level error”. Inherit the class from Activity class. You must include using System.Workflow.ComponentModel to resolve the “Activity”.
Now you need to create the dependency properties to access the input given to the activity or to use the current library id and current item.
Definition: Dependency properties provide a centralized repository of a workflow’s state.Dependency Property provide a mechanism that allows SharePoint Designer to provide values for your custom action before you add your custom functionality.
To allow SharePoint Designer to pass values to your custom action when the workflow runs, you must create public properties inside your custom action.
The dependency properties can be created as:

/// <summary>
/// The dependency property for List ID.
/// </summary>
public static DependencyProperty __ListIdProperty = DependencyProperty.Register(“__ListId”, typeof (string), typeof(CustomWFAction));

Here CustomWFAction is a class name that we have created.

/// <summary>
/// Gets or sets the list or library ID.
/// </summary>
public string __ListId
{
get {
return ((string)this.GetValue(CustomWFAction.__ListIdProperty));
}
set {this.SetValue(CustomWFAction.__ListIdProperty, value); }
}

This property will provide you the current list or library id as a Guid on which workflow is running on. In the similar you can create required dependency properties and get/set their values. You can create a dependency property for Workflow Context, List litem id or any specific input that will be given to the workflow activity.
Now we are ready with the inputs to access the list or library with the current item ID on which workflow is running and the current context of the workflow (Create the dependency property for workflow context). We need to override the “Execute” method to execute the code.
The code for Execute () goes here.

/// <summary>
/// Write the actual code to be executed here.
/// </summary>
Protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
{

    //Write the code here that is to be executed by activity.
    //Ex: Update the item title

 SPWeb web=__Context.Web;
 string listID= __ListId;
 int itemID = __ListItem;
 SPList lstDemo = web.Lists[new Guid(listID)];
 SPListItem lstItemDemo = lstDemo.GetItemById(itemID);
 if (lstDemo.Fields.ContainsField(“Title”))
{
  lstItemDemo[“Title”] = “Demo Activity executed”;
}
 lstItemDemo.Update();
 return base.Execute(executionContext);
}

Add WSS.actions file
Now we need to create the action file for the activity. Right click on the project and click on the add button and then select the “SharePoint Mapped Folder” which maps to workflow folder as shown below:


Now right click on the workflow folder and then add the new text file. Rename the text file with .actions extension.
Add the following code in the file and save it.

<? xml version=”1.0″ encoding=”utf-8″?>
<WorkflowInfo Language=”en-us”>
<Actions Sequential=”then” Parallel=”and”>
<Default>
<RuleDesigner Sentence=”Run action %1″>
<FieldBind Id=”1″ Function=”true”/>
</RuleDesigner>
</Default>
<Action Name=”UpdateItem” ClassName= ” My_CustomActivity.CustomWFAction”
Assembly=” IBSP_CustomActivity, Version=1.0.0.0, Culture=neutral, publicKeyToken = 87cb7fe4b26ece9b” Category=”SharePoint 2010 Actions” AppliesTo=”all” UsesCurrentItem=”true”>
<RuleDesigner Sentence=”Perform demo action”>
</RuleDesigner>
<Parameters>
<Parameter Name=”__Context” Type=”Microsoft.SharePoint.WorkflowActions.WorkflowContext, Microsoft.SharePoint.WorkflowActions” Direction=”In”/>
<Parameter Name=”__ListId” Type=”System.String, mscorlib” Direction=”In” />
<Parameter Name=”__ListItem” Type=”System.Int32, mscorlib” Direction=”In” />
</Parameters>
</Action>
</Actions>
</WorkflowInfo>

Deploy Custom Workflow Action
Now we have workflow activity ready with us. We now need to deploy this activity and use this activity using SharePoint Designer. Update the web.config file on the Site virtual directory folder where we need to create the workflow. Go to the “C:\inetpub\wwwroot\wss\VirtualDirectories\80” location and open the web.config file. Made the following entry to under the “<authorizedRuleTypes>” section.

<authorizedType Assembly=”System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089″ Namespace=”System.Runtime.CompilerServices” TypeName=”ExtensionAttribute” Authorized=”True” />

If you do not register it in web.config file, the action will be visible in workflow actions menu of SharePoint designer but will not get inserted into actual workflow. Restart the IIS after this step. Now Open the SharePoint designer 2010 and open the site where you want to create the workflow. Click on the “Workflows” from the left navigation bar and create the list workflow. Provide the Name and Description of the workflow. Then click on the action menu and now you will see the custom action under the “SharePoint 2010” group that we have created. Select the action and then publish the workflow.

Happy learning. J Have a great day.

No comments:

Post a Comment

5 Steps to achieve Lazy Loading in SharePoint

Hello SharePointers!   Through this blog, I will walk you through a JS library implemented by me which helps us to load SharePoint list it...