I'm trying to programmatically configure the out of the box Approval workflow on a document library using the following code:
SPWorkflowTemplate workflowTemplate = web.WorkflowTemplates.GetTemplateByName(workflowName,
System.Globalization.CultureInfo.CurrentCulture);
SPWorkflowAssociation workflowAssociation =
SPWorkflowAssociation.CreateListAssociation(workflowTemplate, workflowAssocName, workflowWorkList, workflowLogList);
workflowAssociation.AllowAsyncManualStart = false;
workflowAssociation.AllowManual = false;
var associationDataXml = XElement.Parse(workflowAssociation.AssociationData);
workflowAssociation.AssociationData = GetAssociationData(web, associationDataXml);
documentLibrary.WorkflowAssociations.Add(workflowAssociation);
workflowAssociation.Enabled = true;
documentLibrary.DefaultContentApprovalWorkflowId = workflowAssociation.Id;
documentLibrary.Update();
I believe that the GetAssociationData() method returns the correct XML. I have compared the output from the function with the XML set by configuring the workflow manually (using powershell/sharepoint manager). The XML generated GetAssociationData() is actually saved in AssociationData and I can access it after the code have run, there are no exceptions thrown in the code above.
Example of XML:
<!-- generated xml, comment added manually -->
<dfs:myFields xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:dms="http://schemas.microsoft.com/office/2009/documentManagement/types" xmlns:dfs="http://schemas.microsoft.com/office/infopath/2003/dataFormSolution" xmlns:q="http://schemas.microsoft.com/office/infopath/2009/WSSList/queryFields" xmlns:d="http://schemas.microsoft.com/office/infopath/2009/WSSList/dataFields" xmlns:ma="http://schemas.microsoft.com/office/2009/metadata/properties/metaAttributes" xmlns:pc="http://schemas.microsoft.com/office/infopath/2007/PartnerControls" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<dfs:queryFields></dfs:queryFields>
<dfs:dataFields>
<d:SharePointListItem_RW>
<d:Approvers>
<d:Assignment>
<d:Assignee>
<pc:Person>
<pc:DisplayName>SP-GROUP</pc:DisplayName>
<pc:AccountId>SP-GROUP</pc:AccountId>
<pc:AccountType>SharePointGroup</pc:AccountType>
</pc:Person>
</d:Assignee>
<d:Stage xsi:nil="true" />
<d:AssignmentType>Serial</d:AssignmentType>
</d:Assignment>
</d:Approvers>
<d:ExpandGroups>true</d:ExpandGroups>
<d:NotificationMessage />
<d:DueDateforAllTasks xsi:nil="true" />
<d:DurationforSerialTasks xsi:nil="true" />
<d:DurationUnits>Day</d:DurationUnits>
<d:CC />
<d:CancelonRejection>true</d:CancelonRejection>
<d:CancelonChange>true</d:CancelonChange>
<d:EnableContentApproval>true</d:EnableContentApproval>
<d:ApproveWhenComplete>true</d:ApproveWhenComplete>
</d:SharePointListItem_RW>
</dfs:dataFields>
</dfs:myFields>
My problem is that SP-GROUP does not show up in the gui when starting the workflow. Any ideas? Do I have to update the workflow in some way after altering AssociationData? The groups I'm using exists and are working.
Image summary: http://i.imgur.com/68SZ5.png