Thursday, October 6, 2011

Setting Custom Workflow Status with existing and Custom Values

Workflow has few inbuilt status values such as "In Progress", "Completed", "Error Occurred", "Stopped" and "Failed on start". But what if we want to set a custom value for the workflow? A value, which we want the workflow to show whenever it is in progress or completed.

We can set a custom value for the workflow but this can only be done when we are designing our workflows in visual studio. Unfortunately, we cannot do this OOTB or using SharePoint Designer.
This can be done in both, Visual Studio Sequential workflows as well as Visual studio State Machine Workflows. To do this, open your workflow.xml and search for "" tag. Place the cursor just above it and write the below code :


(Please remove "" while writing this in workflow.xml as here it was not accepting the tags)
Now open the designer page and drag a "SetState" activity from your left hand side toolbox. Always Remember, there are two "SetState" activites, one is for setting the state in State Machine workflows and other is for setting the Workflow status to a custom value. The activity with a blue mark in it is used for setting the workflow status. Once you drag the "SetState" activity on the Designer page, right click on it and open its properties. Bind its "State" properties to a variable and select "field" while binding it. Select the correlation token as "WorkflowToken" as this activity does not belong to any task. Now double click the activity and you will come to the page where you need to write some code.

Let us assume that your "SetState" activity name is "PendingState". When you double click on the "SetState" activity, write the below line on the code window : 
PendingState.State= 0; // 0 to 14 are reserved status for workflow.
PendingState.State = (Int32)SPWorkflowStatus.MAX;

The integer value of the MAX is 15. From 0 to 14, the numbers are reserved for internal and builtin values such as InProgress, completed, error occurred etc. In workflow.xml, the value of the first custom value will be 15 (See above as we specified the first custom value as Approved). And then it will keep on increasing by 1. Like if we want to assign the "Rejected" value to the workflow then we will write the below line of code :

PendingState.State = (Int32)SPWorkflowStatus.MAX + 1;

The above line will assign the "Rejected" custom value to the State variable and whenever workflow reaches this state then it will show the status "Rejected". Like this we can write any number of custom values to the workflow. We have used only two custom values in the above Workflow.xml. If we want to use a third one then we will right it just below the "Rejected" custom value like :


The value of "Pending" status will be assigned as follows :

PendingState.State = (Int32)SPWorkflowStatus.MAX + 2;

If you change your workflow.xml file by changing the workflow custom values then deploy it again and run iisreset to see the changes in SharePoint List when the workflow will run.

No comments:

Post a Comment