Month: December 2010

Develop Custom Workflow Activity for SharePoint 2010 Workflow

Home | SharePoint Workflow | Develop Custom Workflow Activity for SharePoint 2010 Workflow

In this article, I’d be discussing the steps to create a custom workflow activity for SharePoint 2010. The need for custom workflow activity arises, when the out-of-box workflow activities do not suffice our requirement. The functionality of developing a custom activity for SharePoint 2010 Workflow is not a new feature in SharePoint 2010. This was available in SharePoint 2007, which is now continued to SharePoint 2010. Once the custom workflow activity is created, it can be made available for use inside SharePoint Designer 2010.

File —> New Project

Select —–> Visual C# —> SharePoint | 2010 —-> Empty Project

custom activity 1

custom activity2

 

 

Click Finish

File —> Add –> New Project

Select the Visual C# | Workflow | Workflow Activity Library and set the project name as ‘CreateActivityDemo’.

custom activity3[5]

Right click ‘CreateActivityDemo’ project and add a reference to Microsoft.SharePoint.dll and Microsoft.SharePoint.Workflow.Actions.dll

Add a new workflow activity called ‘CreateSurveyLibrary.cs’

Switch to view code of ‘CreateSurveyLibrary.cs’

Import the following namespaces

using Microsoft.SharePoint;
using Microsoft.SharePoint.Workflow;
using Microsoft.SharePoint.Workflow Actions;
 
Add a dependency property property by name ‘SiteUrlProperty’
 public static DependencyProperty SiteUrlProperty = DependencyProperty.Register("SiteUrl", 

typeof(string), typeof(CreateSurveyList), new PropertyMetadata(""));
        [DescriptionAttribute("Url of site where survey is to be created")]
        [BrowsableAttribute(true)]
        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
        [ValidationOption(ValidationOption.Optional)]
        public string SiteUrl
        {
            get
            {
                return ((string)(base.GetValue(CreateSurveyList.SiteUrlProperty)));
            }
            set
            {
                base.SetValue(CreateSurveyList.SiteUrlProperty, value);
            }
        }

The SiteUrlProperty is used to capture the url of the site, in which the survey list needs to be created. The convention for creating the dependency property is to have the keyword ‘Property’ appended (at the end) to the name of the actual property. In this case, the name of the regular C# property is SiteUrl, the dependency property is created with convention of ‘SiteUrlProperty’. If this naming convention is not followed, we’ll get a compilation error.

Add another dependency property called ‘SurveyListNameProperty’. This is used to assign the name for Survey List during the list creation.

 public static DependencyProperty SurveyListNameProperty = DependencyProperty.Register("SurveyListName", 

typeof(string), typeof(CreateSurveyList), new PropertyMetadata(""));
        [DescriptionAttribute("Name for survey list")]
        [BrowsableAttribute(true)]
        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
        [ValidationOption(ValidationOption.Optional)]
        public string SurveyListName
        {
            get
            {
                return ((string)(base.GetValue(CreateSurveyList.SurveyListNameProperty)));
            }
            set
            {
                base.SetValue(CreateSurveyList.SurveyListNameProperty, value);
            }
        }
Again, follow the same naming convention for creating this dependency property ‘SurveyListName’, discussed above.

The next logical step in creating custom workflow activity for SharePoint is to override the Execute method of the workflow activity. Then add the relevant logic to the Execute method for creating a Survey List in the specified site with the specified survey list name.

protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        {
            CreateSurveyLibrary();
            return ActivityExecutionStatus.Closed;
        }
 
 
        private void CreateSurveyLibrary()
        {
            using (SPSite oSPSite = new SPSite(SiteUrl))
            {
                using (SPWeb oSPWeb = oSPSite.RootWeb)
                {
                    Guid ID = oSPWeb.Lists.Add(SurveyListName, SurveyListName + System.DateTime.Now.ToString(),
        SPListTemplateType.Survey);
 
                    SPList oSPList = oSPWeb.Lists[ID];
                    oSPList.OnQuickLaunch = true;
                    oSPList.Update();
                }
            }
        }

Create a strong name for the assembly ‘CreateActivityDemo’.

Right click on the CustomWorkflowActivityDemo project and Add —> SharePoint Mapped Folder

Navigate to Template1033Workflow and Select it.

custom activity4

Create an XML file called ‘CreateActivityDemo.Actions’ under the workflow folder. Complete the definition of ‘CreateActivityDemo.Actions’.The definition of ‘.Actions’ file is responsible for making the custom workflow activity appear in the SharePoint Designer 2010.
<WorkflowInfo>
  <Actions Sequential="then" Parallel="and">
    <Action Name="Create Survey List"
        ClassName="CreateActivityDemo.CreateSurveyList"
        Assembly="CreateActivityDemo, Version=1.0.0.0,
           Culture=neutral, PublicKeyToken=38b1d60938e39f46"
        AppliesTo="all"
        Category="Sundar Activity">
      <RuleDesigner Sentence="Survey List Name %1 to site %2.">
        <FieldBind Field="SurveyListName" Text="Survey List Name"
           DesignerType="TextArea" Id="1"/>
        <FieldBind Field="SiteUrl" Text="Url of base site" Id="2"
           DesignerType="TextArea"/>
      </RuleDesigner>
      <Parameters>
        <Parameter Name="SurveyListName" Type="System.String, mscorlib"
      Direction="In" />
        <Parameter Name="SiteUrl" Type="System.String, mscorlib"
      Direction="In" />
      </Parameters>
    </Action>
  </Actions>
</WorkflowInfo>

 

Next, we need to add the assembly for custom workflow activity in the Package.

Double click on the Package.package

custom activity5
Click ‘Advanced’ in Package Designer.
Add a Safe Control for Workflow Activity Assembly ‘CreateActivityDemo’.
custom activity6
 
Create an authorized type entry for the CreateActivityDemo assembly (custom workflow activity assembly).
<authorizedType Assembly="CreateActivityDemo, Version=1.0.0.0, Culture=neutral, PublicKeyToken=38b1d60938e39f46" Namespace="CreateActivityDemo"
 TypeName="*" Authorized="True" />
Deploy the Solution. Now we’ll see the custom activity ‘Create Survey List’ appearing in the SharePoint Designer 2010 under actions.

custom activity7

 

 

The custom workflow activity  ‘Create Survey List’ is ready to be tested.

 

Create a re-usable workflow and drop the ‘Create Survey List’ activity.

 

Set the Values for SiteUrl and SurveyListName.

custom activity8[5]

 

Start the Workflow.

 

custom activity 9

 

Now the Survey is created in the site with defined name.

 

custom activity 10

This completes the development of custom workflow activity.

 Subscribe to my post