Friday, July 15, 2011

Custom Workflow Activity for SharePoint Designer 2010

Back in Moss 2007 we had the feature of creating a new custom activity for SharePoint designer. I never tried that. Yesterday I had to present a POC to a customer on Custom activity for SharePoint designer 2010. The requirement is pretty simple.

User will be presented with a simple list. On creating of a new item, a approval workflow will be triggered. On approval, a new subsite has to be created. So I thought creating a custom activity in designer and using that to create a designer workflow would to do the trick.

I will describe with steps to create a custom activity for a SharePoint designer 2010 below. Since creating a approval workflow is simple I m not going to cover it :-)

1. Open Visual Studio
2. Select the “File->New->Project”
3. For template, select “Workflow->Workflow Activity Library” ( If you don't see this template, then target your environment to .Net 3.5.)
4. For Name, type “CustomActivities”
7. Rename Activity1.cs to CustomAction.cs
8. Right click the project, select “Add Reference”
9. Add the following references:
o C:\program files\common files\microsoft shared\web server
extensions\14\isapi\Microsoft.SharePoint.dll

I dont want to jump into full code directly. Lets go Step by Step.

Create a new function which creates new site




To make your custom code we need to override the Execute method of System.Workflow.Activities.
Call our CreateSite() function within the Execute method.



We have created a simple activity which is good enough to create a new SharePoint site. But if you notice I have hardcoded the siteTitle and siteURLRequested. Not good practice ah..

Lets create a new set of properties to get and set values for our title and url parameters. Before that to make the properties to be picked by the SharePoint designer we need to create Dependency properties as below.


   public static DependencyProperty TaskTitleProperty = DependencyProperty.Register("TaskTitle", typeof(string), typeof(CustomAction));

        [Category("Cross-Site Actions"), Browsable(true)]
        [DesignerSerializationVisibility
          (DesignerSerializationVisibility.Visible)]
        public string TaskTitle
        {
            get
            {
                return ((string)
                    (base.GetValue(CustomAction.TaskTitleProperty)));
            }
            set
            {
                base.SetValue(CustomAction.TaskTitleProperty, value);
            }
        }

Note that we need to prefix the DependencyProperty with the word "Property" else VS2010 is going to shout at you.

Now use the dependency property created in our CreateSite method. Overall code looks like below.


So create another dependency property for siteUrlRequested as well which i m not doing here. Sign the assembly and build it. Place the assembly in GAC using gacutil.exe(never  install manually).

We are done with the coding part. Now lets register our newly created activity with the SharePoint designer.

If you navigate to 14 hive folder under 1033/Workflows/ you will find MOSS.ACTIONS and WSS.ACTIONS.

As a good housekeeping we will create a new ACTIONS file. Say CustomAction.ACTIONS.

Place the below xml in it to register our custom activity.


<WorkflowInfo>
  <Actions Sequential="then" Parallel="and">
     <Action Name="Create Site"
    ClassName="CustomActivities.CustomActions"
    Assembly="CustomActivities, Version=1.0.0.0, Culture=neutral, PublicKeyToken=a3170baa21b0a3e9"
    AppliesTo="all" Category="Custom Site Actions">
        <RuleDesigner 
           Sentence="Create a Site titled %1">
           <FieldBind Field="TaskTitle" DesignerType="TextArea" 
              Id="1"/>
        </RuleDesigner>
         <Parameters>
            <Parameter Name="TaskTitle" Type="System.String, mscorlib" 
              Direction="In" />
         </Parameters>
     </Action>
  </Actions>
</WorkflowInfo>

Save the file and close.  We have a very last step that to register our dll as a Authorized type with my webapplication. Navigate to web.config in the file system and make the below entry.

<authorizedType Assembly="CustomActivities, Version=1.0.0.0, 
   Culture=neutral, PublicKeyToken=a3170baa21b0a3e9"     
   Namespace="CustomActivities" TypeName="*" Authorized="True" />
Save the web.config. There you go..
Perform and IISReset and load the designer. You will see a new activity added under "Custom Site Actions"

No comments:

Post a Comment