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

Create SharePoint 2010 workflow using SharePoint Designer

In this article, I’d be discussing about the creating SharePoint 2010 workflows using SharePoint Designer 2010. Rather than designing a workflow from scratch, I’d be re-using the workflow defined in Visio (Refer previous article).

Before getting into the workflow, I’m defining a content type by name ‘Invoice Report’ with three basic site-columns InvoiceAmount, Invoice Date and InvoiceDepartment to keep it simple.

site columns

content type

 Create a new custom list by name ‘Invoice’ and attach the content type ‘InvoiceReport’ to it.

list

Navigate to Site Objects –> Workflow in SharePoint Designer

import from visio1[6]

 Choose ‘invoice processing.wvi’ (workflow designed in the previous article) and click Next. 

import from visio2

Choose ‘Reusable workflow’ as type of workflow (in this case i want this workflow to be re—used across lists that has associated invoice report content-type).

import from visio3

Click finish.

The workflow will look like the following in the designer.

workflow designer

Go to step ‘Log for starting’ and change the message to ‘workflow is starting’’.

Go to If  statement under step ‘Invoice > 50K’ and define the expression like if Invoice Amount less than or equal to 50,000.

image

  image

Set Workflow status to Approved under If loop.

image

Under log for Auto Approval, set the message ‘Workflow is Auto Approved’.

image

Go the Else part and configure the Task Approval process. 

approval config

In this case, the Task Approval process will be executed if the invoice amount is greater than 50,000. The idea is to enforce a manual task approval process, approved by the manager.

The workflow design would look like the following:-

final workflow design

If you choose Custom Approval Process or Custom Task Process, you’ll get bunch of actions to customize the behavior of the single task or task process.

task behavior1

task behavior2

The Task Behavior Actions is one of the improved feature in SharePoint 2010 workflows. The Task behavior actions does not appear in Visio 2010, when you design workflows . It is available in SP Designer 2010 and VS 2010. The actions like Escalate Task may find a common usage. But in this case, I’m not customizing any task behavior, just sticking with the task behavior defined for the Approval Process in SP Designer 2010.

Check for errors and publish the workflow.

Go to the SharePoint site and associate the Invoice Processing workflow with the invoice list.

Under workflow settings –> Add a workflow

Set content type to ‘Invoice Report’

Select ‘invoice processing’ under the workflow template

Set ‘Department Invoice workflow’ for the unique name of the workflow.

Set the workflow to be started manually

workflow setting

Create an invoice item in the list.

invoice new item

Start the workflow manually.

The invoice item would be approved, because the amount is less than 50K.

image

Let’s try with another invoice amount of 60, 000.

Start the workflow again.

Now the workflow is not auto-approved, task is assigned to the Manager and it is in progress state.

Log in as Manager to see the assigned Task.

task approval

Click ‘Approval’ button to approve the invoice.

We’ve created a very simple workflow in SharePoint Designer 2010. I’ll deal with more advanced workflow scenarios in the next articles.

 Subscribe to my post

Design SharePoint 2010 workflows in Visio 2010

I thought of writing a series of articles around SharePoint 2010 workflows.  This is one of my favourite area in SharePoint 2010 and  I’m also involved in lot of  SharePoint 2010 workflow development these days. Microsoft Visio 2010 Premium edition has the ability to design SharePoint 2010 workflows using the visual designer and export it to SharePoint Designer 2010 (and then to Visual Studio 2010) for further implementation. This brings powerful workflow modelleing capability to the business users and also brings consistency during workflow modeling and workflow implementation.

The business users/power users can start modeling the business process in Visio 2010 and then transition this to the development team for further implementation. The workflow modeling in Visio 2010 needs a bit of understanding of SharePoint 2010 platform. At this point of time, the SharePoint 2010 workflow modeling capability is not supported in Professional edition of Visio 2010.

 Let’s open the Visio 2010

File —-> New —> Microsoft SharePoint Workflow —> Click ‘Create’

We’ll get to see three tabs SharePoint Workflow Actions, SharePoint Workflow Conditions and SharePoint Workflow Terminators. 

image

SharePoint workflow terminators has the activities for start and terminate of workflow.

image

SharePoint workflow conditions has the set of activities to perform various conditonal checks like comparing against arbitrary parameters, comparing with the current list items and  check for other SharePoint related objects.

image

SharePoint workflow actions has the whole lot of actions that can be performed related to a SharePoint workflow.

image

If we do quick tour of activities present in Visio 2010, we feel most of these are available in SharePoint Designer 2010. Yes, SharePoint Designer 2010 and Visual Studio 2010 has lot many activities. I’ll brief the in later sections of that article. Let’s move on the the business process, that we want to model in Visio 2010. It’s a simple one.  Let’s assume that we’re modeling an Invoice process for a contracting form.  The contracting firm (vendor) submits invoice to the company ‘A”. If the invoice is less than 50K, the invoice would be approved. A manual approval process is needed if the invoice exceeds 50k.

Let’s try to model this.. 

1. Drag the ‘Start’ activity from the workflow terminators

2. Drag ‘Log to History’ from Workflow Actions and connect with the ‘Start’ activity. Double click the activity and name it as ‘Log for starting’

3. Drag ‘Compare Data Source’ activity from workflow conditions and connect it with ‘’Log to History’ activity.  Double click and rename the activity as ‘If invoice > 50k’

4. Drag ‘Set Workflow status activity’ . Connect it with the compare data source activity (‘invoice > 50k’). Double click and rename the activity as ‘Auto Approval’. Right click on the connector line and set it as ‘Yes’.

5. Drag ‘Start approval process activity’ to the other side of the compare data source activity (‘invoice > 50k’) and connect it. Double click and name it as ‘Manager approval process’. Right click on the connector and set it as ‘No’

6. Drag a ‘Log to History’ next to ‘Auto Approval’ and double click and name it as ‘Log for Auto approval’. Connect it with Terminate activity.

 

 image

Navigate to the Process Tab in the Ribbon and click ‘Export’

image

Save the workflow definition as ‘invoice processing.vwi’.

Now the workflow is ready for implementation in Microsoft SharePoint Designer 2010.

Note:-

We’ve included the manual ‘Approval Process’ if the invoice > 50 K . This involves task process. The task behavior cannot be customized in Visio 2010 to include more custom task behavior actions like Wait for Change in Task Process, Wait for Deletion in Task Item Process and Escalate Task etc. This can be customized

I’ll discuss about implementing this workflow in SharePoint Designer 2010 in the next article.

 Subscribe to my post

Worklow activities not supported by SharePoint 2010

For the past few days i’m exploring more into the Worklow Development with SharePoint 2010. I’ve developed applications using Windows Workflow Foundation in the past. Based on that i tried to compare whether the SharePoint workflow framework delivers all the capabilities offered by Windows Workflow foundation. I’ve learnt that the following activities are not supported . 

Activity

Description

InvokeWebService

Invokes a web service from workflow

InvokeWorkflow

Invokes another workflow

Policy

Stores the definition and execution logic of a RuleSet

Receive Activity

Exposes workflow as WCF service

WebServiceInput

Used for exposing workflow as web service

WebServiceOutput

Used for exposing worklow as web service

WebServiceFault

Sends a fault from workflow to web service

Send Activity

Connects to WCF end point

Suspend

Suspends the execution fo Workflow

Throw

Raises exception in workflow

SynchronizationScope

Synchronizes the execution

TransactionScope

Groups activities inot a transaction scope

Compensate

Undo or componesate  work that was previously done

CompensatableTransaction

Uses compensation logc

CompensatbleSequence

Same purpose as compensate activity

 Even though these activities get listed in Visual Studio Toolbox, they are not suppored in SharePoint Workflows

 Subscribe to my post