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

Published Sun, Dec 26 2010 16:31 by lavssun
Filed under:

Comments

# re: Develop Custom Workflow Activity for SharePoint 2010 Workflow

where do you creat  your CreateSurveyList ? Is it a class?

Wednesday, April 13, 2011 8:40 AM by me

# re: Develop Custom Workflow Activity for SharePoint 2010 Workflow

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.

Friday, May 06, 2011 9:44 AM by a visitor

# re: Develop Custom Workflow Activity for SharePoint 2010 Workflow

I've the same problem as one reported above. it shows up in Actions list but wont add up in the workflow Step.

Friday, May 13, 2011 5:38 AM by sp-freak

# There is somthing wrong with your code

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

Friday, May 13, 2011 6:00 AM by Loogares

# re: Develop Custom Workflow Activity for SharePoint 2010 Workflow

I follow above steps and create same.

but i couldn't add action in workflow.

Monday, May 16, 2011 4:08 AM by SPdemo

# re: Develop Custom Workflow Activity for SharePoint 2010 Workflow

Could you create a project for this, so I will be able to download it?  Thank you in any case!

Tuesday, May 24, 2011 9:37 AM by Yaroslav

# re: Develop Custom Workflow Activity for SharePoint 2010 Workflow

Me, too!

Is there a source code for download?

Thursday, May 26, 2011 12:46 PM by Ronald

# re: Develop Custom Workflow Activity for SharePoint 2010 Workflow

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:\Projects\CVS-visual-studio-projects\TestWorkflowAction\TestWorkflowAction\Package\Package.package TestWorkflowAction

Friday, May 27, 2011 6:29 AM by MKI

# re: Develop Custom Workflow Activity for SharePoint 2010 Workflow

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.

Thursday, June 02, 2011 9:19 AM by Sebastian Rogers

# re: Develop Custom Workflow Activity for SharePoint 2010 Workflow

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.

Sunday, July 17, 2011 6:10 AM by thsun

# re: Develop Custom Workflow Activity for SharePoint 2010 Workflow

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.

Monday, August 01, 2011 3:41 AM by Voicu Seiche

# re: Develop Custom Workflow Activity for SharePoint 2010 Workflow

Hey thanks for the article!  This was a great introduction to creating custom activities for SharePoint Designer.

Thursday, September 08, 2011 10:39 AM by Blake

# re: Develop Custom Workflow Activity for SharePoint 2010 Workflow

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:\windows\Assembly\CreateActivityDemo.dll.

Monday, November 07, 2011 8:38 PM by Kamesh

# re: Develop Custom Workflow Activity for SharePoint 2010 Workflow

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.

Tuesday, November 08, 2011 1:48 PM by Kamesh

# re: Develop Custom Workflow Activity for SharePoint 2010 Workflow

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.

Tuesday, November 29, 2011 4:07 AM by Pankaj

# re: Develop Custom Workflow Activity for SharePoint 2010 Workflow

I too am unable to use the action, although it dooes appear in SharePoint designer selecting it doesn't do anything.

Has anyone managed to resolve this?!

Monday, February 20, 2012 9:28 AM by Dina G

# re: Develop Custom Workflow Activity for SharePoint 2010 Workflow

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.

Monday, February 20, 2012 9:38 AM by Dina G

# re: Develop Custom Workflow Activity for SharePoint 2010 Workflow

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......

Saturday, March 24, 2012 9:29 AM by Danial

# re: Develop Custom Workflow Activity for SharePoint 2010 Workflow

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.

Monday, June 04, 2012 2:20 AM by pazhanikumar

# re: Develop Custom Workflow Activity for SharePoint 2010 Workflow

If you have an app server and web front ends, be sure to update the web.configs on each server.

Friday, August 31, 2012 5:11 AM by Daryl

# re: Develop Custom Workflow Activity for SharePoint 2010 Workflow

A good reference on how to add the safe entry with minimum hard-coded information:

msdn.microsoft.com/.../ff798302.aspx

Registering Workflow Activities for Declarative Workflows

Monday, October 15, 2012 4:34 AM by Half

Leave a Comment

(required) 
(required) 
(optional)
(required) 
If you can't read this number refresh your screen
Enter the numbers above: