Creating the Custom Activity for SharePoint Designer 2010

 

1. Introduction

A workflow is a set of Actions that you want to do to resolve your business (Go to this link : http://office.microsoft.com/en-us/sharepointdesigner/HA101005871033.aspx). With Microsoft SharePoint Designer you can design workflows that add application logic to your site or application without having to write custom code. Using the Workflow Designer, you create the rules that associate conditions and actions with items in SharePoint lists or libraries, so that changes to items in list or libraries trigger action in the workflow. Ex.: You can design a workflow to automate the business process in you company like vacation request, leave office request….SharePoint Designer will help you create a workflow rapidly.

But sometime, the default Actions that provided by SharePoint Designer is not enough or not adapt with business requirements like query user data from Active Directory, query data from other web site….To resolve this problem, you need to create custom activity.

2. Creating Custom Activity

In the following example, I will create a custom activity that translates the input text from a language to another language. Example: translate text from English to German…

The Google Translator Engine will be used to translate.

2.1 Create Project

To create a custom activity, you should create an Activity Library project, with name MCWorkflowActivitiesclip_image002

And add new Activity with name: TranslateField

clip_image004

Select the “Activity with definition and user code in same code file” type

clip_image006

2.2 Coding

The Visual Studio will create 2 files:

- TranslateField.cs

- TranslateField.Designer.cs (Just ignore this file – it use for design).

You need to “Add Reference…” to Microsoft.SharePoint.dll and microsoft.sharepoint.WorkflowActions.dll. These files are located in: C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI\

Create Dependency Properties

Properties in the activity are exposed in the workflow designer in Office SharePoint Designer 2007, or the Workflow Designer in Visual Studio, and are populated only when the workflow instance is started at run time. You create and expose these properties by declaring them as type DependencyProperty. Following is the code example for the exposing the FromLanguage property in the TranslateField activity.

clip_image007

The Execute Method

The Windows Workflow run-time engine calls the Execute method of the activity. This method is inherited from the base Activity class; this is where you write the business logic for the activity. The following code example connects to Google Translator Engine and provides the parameters for translating and gets the returned value in other language.

clip_image008

The Execute method returns an ActivityExecutionStatus value, this indicates that the work is finished.

.ACTIONS File

clip_image009

The workflow designer in Office SharePoint Designer 2010 communicates with the server running SharePoint Server to retrieve an XML file that contains the options for the user to define conditions and actions while creating the steps of the workflow. These files are language-dependent, and are stored in the path \14\TEMPLATE\1033\Workflow. The WSS.ACTIONS file contains the files that are provided with SharePoint Foundation 2010. You create an additional file (for example, one named MondayCoffee.ACTIONS) that contains the metadata for your custom activity to expose in Office SharePoint Designer 2010. Following is the XML for this file.

Authorizing the Custom Workflow Activity

You must register the assembly that contains the custom activity as an AuthorizedType in the web.config file that is at the root of the Internet Information Services (IIS) server Web Application folder. Following is the entry to add in the authorizedTypes section.

clip_image010

2.3 Building and deploy

Let sign the assembly with mckey.snk and deploy the MondayCoffee.WorkflowActivities.dll in to the GAC.

3. Using TranslateField activity in SharePoint Designer 2010.

Open the SharePoint Designer and creating a workflow:

In the Step 1, let type MC and select “MC Translate Text (Using Google Translator)

clip_image012

We have the MC Translate Text:

clip_image014

The “MC Translate Text” will get the text as input: “this text

And language:
clip_image016

To language:
clip_image018

And call to Google Translator Engine to translate the input text.

The Custom Activity will return translated text into variable: “variable2

Then we will use the translated text stored in variable2, and using another Activity to update the List Item

clip_image020

Save the Workflow and Publish into the SharePoint Site.

4. Run Workflow.

We have a ListItem with the content in English is: “With Microsoft Office SharePoint Designer 2010, you can design workflows that add application logic to your site or application without having to write custom code.”

clip_image022

Now we run Workflow for this item to translate the text into German

Select MC Translator and Start it.

clip_image024 clip_image026

Now the text has been translated into German

clip_image028

Comments

Popular posts from this blog

Setup SharePoint 2010

Register CSS to SP Master Page