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

24 thoughts on “Develop Custom Workflow Activity for SharePoint 2010 Workflow

  1. Thanks for the article, I have got to the point where i can see my custom activity in the insert action drop down, however when i click on it it does not get added to the workflow step. There are no errors it just wont add it. Any help would be much appreciated.

  2. I think you have something wrong in your code, it’s been impossible to me to recreate it, could you provide a simple example on how to create a simple list with a custom workflow action?

    Thanks a lot

    Loogares

  3. i always got this error

    Error 1 Both “TestWorkflowAction.csproj” and “TestWorkflowAction” contain a file that deploys to the same Package location: TestWorkflowAction.dll D:ProjectsCVS-visual-studio-projectsTestWorkflowActionTestWorkflowActionPackagePackage.package TestWorkflowAction

  4. Hi a visitor – the custom activity will NOT show up unless you ensure that the authorisedType is added to the web.config file of the Sharepoint Web Application.

  5. I’ve added the authorisedType to web.config. The custom activity ‘Create Survey List’ appears in the SharePoint Designer 2010 under actions but when I try to add it, nothing happens.

  6. Nice article, but it’s some errors:
    – You can’t have a class name (CreateSurveyLibrary) and a method with the same name !!! This creates a compilation error.
    – In the .action file, the class is called ‘CreateSurveyList’.
    – The ‘authorizedType’ line is not specified where to put it (for others: this line is put in the web.config of the target web application).
    – Also, it is worth mentioning that the public key token must be changed, because it’s not the same if you create 2 different assemblies. Here it would be helpful if a solution with these projects would be available for download.

  7. Thanks for this article. This is a great introduction and works for me. Just neet to modify the Public key token “38b1d60938e39f46” with the one in c:windowsAssemblyCreateActivityDemo.dll.

  8. Can we debug this activity. I have written a Activity to copy an item from one site to another. When I run the workflow it simply throws a object reference exception. Any help in debugging would be appriciated.

  9. Hi,

    this is very nice article.

    when i tried to create custom action as u mentioned above. it successfully created and added into sharepoint designer workflow action list. but when going to add into workflow from action list,does not add into workflow.

    Please help.

  10. Hello all,

    Ironically after looking at this for 4 hours and then posting a request for help… I have found the problem(!).

    In case it helps anyone in the future, if you see the activity in SharePoint designer but when you select it nothing happens then double check you “authorizedType” setup (under the System.Workflow.ComponentModel.WorkflowCompiler section). You must add in a line to the assembly and correct namespace(!). Having corrected a typo and restarted designer I can now add the activity to my workflow.

    Hope this helps someone.

    Best to all.

  11. This is good introduction for me about the custom workflow.
    I did everything as shown in this article, solution is deployed to the target web application.
    But whenever i create new workflow, i popping up with some error message.
    “The list of workflow actions on the server references an assembly that does not exist. The assembly strong name is CreateActiviyDemo, Version=1.0.0.0, Culture=neutral, PublicKeyToken=38b1d60938e39f47. Contact your sever administration for more.”

    Please help me out with this.

  12. Hi Dina G:

    how to check that the “authorizedType” value. could you explain it briefly.

    “System.Workflow.ComponentModel.WorkflowCompiler section” are you denoting the assembly.

    -John

  13. I followed the same steps above every thing works good but the issue when try to use the action designer nothing is happening…

    help would be more appreciated……

  14. Great blog really it is helpful. I recommend to include some steps like creating strong name for activity library project, First I get error due this strong name while deploying into GAC later I resolved it and deployed sucesfully.

Leave a Reply