Wednesday, November 9, 2011

Programmatically change workflow status to custom status

Sometimes values like In progress, Completed, etc. are not enough and don't match our demands. So I am going to simply describe how to change workflow status to some custom status.
At first Elements.xml file of workflow has to be changed and new custom states have to be added. Put this xml definition into <MetaData>.

<ExtendedStatusColumnValues>
        <StatusColumnValue>Waiting for your approving</StatusColumnValue>
        <StatusColumnValue>Approved and waiting</StatusColumnValue>
  </ExtendedStatusColumnValues>
Then add a new setState activity to workflow and set methodInvoking. The method should look like this:

private void setStateApprovedAndWaiting_MethodInvoking(object sender, EventArgs e)
        {
            this.setStateToApprovedAndWaiting.State = (int)SPWorkflowStatus.Max + 1;


        }

First item in <ExtendedStatusColumnValues> corresponds with  SPWorkflowStatus.Max, other values are available since the exact number is added to the SPWorkflowStatus.Max.

For better understanding read this articles:
http://msdn.microsoft.com/en-us/library/cc514224(office.12).aspx#MOSS2007DevWorkflowSolutions_SettingOutcomeValues

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.workflow.spworkflowstatus.aspx

That all look very nice and simple. Sadly for me my experience wasn't that much simple. If I wanted to use member of SPWorkflowStatus to set workflow status I got this error message: Specified argument was out of the range of valid values.Till know I don't know why. I also had some complex workflow and when I tried to set state to some of my custom states using function above the error occurred. When I set state directly in setState activity the custom state was set. So I would recommend to check and find ID of custom state and then use this number instead of setting state property in the function.
Maybe somebody also had the same problem and solve it, if so please don't hesitate to comment :)
Happy codding....

No comments:

Post a Comment