5

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:

screenshot

2

You need to update the workflow:

// Update Workflow Association Data
wfAssociation.AssociationData = data;
wfAssociation.Enabled = true;

// Update Workflow Association
documentLibrary.WorkflowAssociations.Update(wfAssociation);

I wrote a blogpost about this topic with a possible solution, maybe it could be handy: http://hevling.be/blog/change-properties-of-a-sharepoint-workflow-instance/

  • I found the error, the code was working but I had a tiny error in the XML. That's those hours wasted.. – Vegard Feb 10 '12 at 14:04
  • 1
    To avoid such errors you can set up approvers through UI and then grab generated xml using sharepoint manager. – Sergei Sergeev Feb 10 '12 at 19:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.