Skywalker3171

Developer´s SharePoint blog (SharePoint, Workflows, Apps, Document Management Systems, etc…)

SharePoint Mini Workflow

Posted by skywalker3171 on March 13, 2012

If you want to develop your own Custom Approval Workflow for SharePoint instead of using the Out Of Box Workflow, which have limited flexibility and may seem overloaded if decide to import them into Visual Studio, all you have to do is is follow a few simple steps.


First, for demonstration purposes let us assume that the list which will be attached to the Workflow has a status column. This status column is to be updated, when the associated approval task is set to “Completed”.

Create a new Project using Visual Studio and use the proposed History list and Task list.

The workflow should kick in, when the user starts it by using the item menu.

Fist thing to do is drag a “createTask” activity into the Sequential Workflow. As explained by Thor, take care to assign a different Correlation Token than the workflow’s main Correlation token.

Two things need to be set by clicking the ellipse in the Task’s property menu, the TaskId and the TaskProperties. For the TaskProperties click the “Bind to a new member” tab and fill in a new member name, “TaskProps1”, if you take the proposed name from Visual Studio, strangely it will not work. Then click “Create Property” and ok.

Next, do the same thing for the TaskId, and call it “TaskId1”. Now we need to insert a While block, which will have a TaskChanged activity in it. Behind the block goes a simple code activity.

Now for some code. Here’s the snippet to assign the newly generated task to Joe, inside the createTask1_MethodInvoking method. In a more elaborate scenario, the approver names could be taken from a seperate workflow configuration list, with a couple of conditions attached.

private void createTask1_MethodInvoking(object sender, EventArgs e)
{
CreateTask task = sender as CreateTask;
task.TaskId = Guid.NewGuid();

TaskId1 = Guid.NewGuid();
TaskProps1 = new SPWorkflowTaskProperties();
TaskProps1.Title = “Generic Approval Task”;
TaskProps1.AssignedTo = “win-9mo894oo9mf\\joe”;
TaskProps1.Description = “Please Approve”;
}

The other snippet changes the status to the original “doclib” list to Completed, when the Approver (in this example just a single approver), sets his task to completed, whatever percentage he enters is irrelevant. This goes into codeActivity1 after the while block.

private void codeActivity1_ExecuteCode(object sender, EventArgs e)
{
workflowProperties.Item["status"] = "Completed";
workflowProperties.Item.Update();
}

Lets not forget the all important While condition. This is to ensure that the while block will be activated by the SharePoint Workflow Engine every time the appropriate Task is changed, until the status is set to completed.

this.workflowProperties.Item.Tasks.Count == 0 || !this.workflowProperties.Item.Tasks[0]["ows_Status"].Equals("Completed")

Now try starting the workflow, by clicking Workflow for a document in the doclib list in the items popup menu.

Then go to the Task list for the assigned Approver (in this case joe) and set the Task to completed. Make sure to save your changes.

Success, the document itself displays the new status.

Leave a comment