Code to Associate an Approval Workflow and Make it the Default for Content Approval

Kind of random, but I had this laying around and I figure it might be useful to someone. I got the XML by configuring an Approval workflow and using SharePoint Manager to copy the property value. Note: this assumes you are running MOSS and already have a Tasks list and Workflow History list.

static
string _associationXml = @"

<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>A Site Members</my:DisplayName>

<my:AccountId>A Site Members</my:AccountId>

<my:AccountType>SharePointGroup</my:AccountType>

</my:Person>

</my:Reviewers>

<my:CC></my:CC>

<my:DueDate xsi:nil=’true’></my:DueDate>

<my:Description>Please review this document.</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>false</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>";

 

static
void Main(string[] args)

{


//Get the site, web, and list


SPSite site = new
SPSite(@"http://YourUrlHere");


SPWeb web = site.RootWeb;


SPList docs = web.Lists["Your List Here"];

 


//Get the workflow template


SPWorkflowTemplate approvalTemplate =

web.WorkflowTemplates.GetTemplateByName("Approval",

System.Globalization.CultureInfo.CurrentCulture);

 


//Create the association


SPWorkflowAssociation assoc =


SPWorkflowAssociation.CreateListAssociation(

approvalTemplate,


"Approval Workflow",

web.Lists["Tasks"],

web.Lists["Workflow History"]);

 


//Set the startup options

assoc.AllowManual = true;

assoc.AutoStartCreate = true;

 


//Provide the association data

assoc.AssociationData = _associationXml;

 


//Apply the association to the document library

docs.AddWorkflowAssociation(assoc);

 


//Enable moderation and make the workflow the


//default approval workflow

docs.EnableModeration = true;

docs.EnableVersioning = true;

docs.EnableMinorVersions = true;

docs.DefaultContentApprovalWorkflowId = assoc.Id;

docs.Update();

 


Console.WriteLine("Done!");


Console.ReadLine();

}

Author: Doug Ware