Programmatically setting up Approval workflow in MOSS
June 27, 2008
Out-of-box Approval works pretty well for simple content approval and that’s what exactly I wanted. If you attach an Approval workflow to a list from the UI you get number of options to set such as Approvers, People who needs to be notified and an important one if you want to update the approval status of a list item to Approved or Rejected is “Update the approval status (use this workflow to control content approval)” in “Post completion Workflow Activities”
As in most the real world situation I want to set this up during deployment process and not from the UI.
Following code snippet will set up your list with Moderation enabled:
SPList list = web.Lists["MyList"]; list.EnableModeration = true; list.Update();
Following code snippet sets up the Approval work flow on the list
SPWorkflowTemplate baseTemplate = web.WorkflowTemplates.GetTemplateByName("Approval", CultureInfo.InvariantCulture);
SPWorkflowAssociation assoc = SPWorkflowAssociation.CreateListAssociation(baseTemplate, "Property Approval", web.Lists["Workflow Tasks"], web.Lists["Workflow History"]);
assoc.AllowManual = true;
assoc.AutoStartCreate = true;
string data = GetApprovalWorkFlowData();
assoc.AssociationData = data;
list.AddWorkflowAssociation(assoc);
In Above code snippet AssociationData property of SPWorkflowAssociation can be used to set things like Approvers, People who needs to be notified and “Post completion Workflow Activities” for content approval
AssociationData is in XML format and looks like:
<my:myFields xml:lang="en-us" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD">
<my:Reviewers>
<my:Person>
<my:DisplayName>Ketul Patel</my:DisplayName>
<my:AccountId>domain\kpatel</my:AccountId>
<my:AccountType>User</my:AccountType>
</my:Person>
</my:Reviewers>
<my:CC>
<my:Person>
<my:DisplayName>Joe Doe</my:DisplayName>
<my:AccountId>domain\jdoe</my:AccountId>
<my:AccountType>User</my:AccountType>
</my:Person>
</my:CC>
<my:DueDate xsi:nil="true"></my:DueDate>
<my:Description>A new Property has been added to the system and requires approval.</my:Description>
<my:Title></my:Title>
<my:DefaultTaskType>1</my:DefaultTaskType>
<my:CreateTasksInSerial>true</my:CreateTasksInSerial>
<my:AllowDelegation>true</my:AllowDelegation>
<my:AllowChangeRequests>true</my:AllowChangeRequests>
<my:StopOnAnyReject xsi:nil="true"></my:StopOnAnyReject>
<my:WantedTasks xsi:nil="true"></my:WantedTasks>
<my:SetMetadataOnSuccess>false</my:SetMetadataOnSuccess>
<my:MetadataSuccessField></my:MetadataSuccessField>
<my:MetadataSuccessValue></my:MetadataSuccessValue>
<my:ApproveWhenComplete>true</my:ApproveWhenComplete>
<my:TimePerTaskVal xsi:nil="true"></my:TimePerTaskVal>
<my:TimePerTaskType xsi:nil="true"></my:TimePerTaskType>
<my:Voting>false</my:Voting>
<my:MetadataTriggerField></my:MetadataTriggerField>
<my:MetadataTriggerValue></my:MetadataTriggerValue>
<my:InitLock>false</my:InitLock>
<my:MetadataStop>false</my:MetadataStop>
<my:ItemChangeStop>false</my:ItemChangeStop>
<my:GroupTasks>false</my:GroupTasks>
</my:myFields>
Various elements in the above XML are pretty self explaining and roughly maps to relevant tick box or textbox on the “Customize Workflow” Page (CstWrkflIP.aspx)
<my:ApproveWhenComplete>true</my:ApproveWhenComplete>
Is the element to use to set the “Update the approval status (use this workflow to control content approval)” checkbox
Entry Filed under: MOSS, Workflow. Tags: Add new tag, MOSS, Workflow.
4 Comments Add your own
Leave a Comment
Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
Trackback this post | Subscribe to the comments via RSS Feed

1.
per Høyer | August 5, 2008 at 2:30 pm
Tanks so vry much, this is exactly what i was looking for.
2.
Mr Big | October 20, 2008 at 7:48 pm
This looks good. The one part missing for me is how do I deploy this? Also, when is this code executed?
Thanks,
Big
3.
pateketu | October 21, 2008 at 9:48 am
Big, You can write a Feature receiver and deploy it on Feature Activation, google for Feature Receivers if you are not familiar with feature framework
If your question is when the set-up code is executed than it can be executed when you activate the Feature
4.
Ian | July 1, 2009 at 7:49 pm
Tried the programmatic workflow, worked like a charm, thanks for doing such a neat, well put together post.