5

I've written some code to add a couple of workflows to an existing document library. Basically, it's not more then these lines of code:

                // Create an association for the default approval workflow
            workflowAssociation =
              SPWorkflowAssociation.CreateListAssociation(approvalWorkflowTemplate, "Approval", taskList, historyList);
            list.WorkflowAssociations.Add(workflowAssociation);

When I execute this and then check the settings of the list, I can see the workflow, added under All/Everything (not sure what the correct translation is). But when I select a document, in this case a PDF file (but that shouldn't matter I think), it says there are no workflows available for starting. I've also checked the workflow settings to see if it's allowed to start the workflow manually, which is the case. And of course I'm logged in as a user with enought rights to actually start the workflow. Any ideas what I'm missing here?

Update: When I manually add the workflow to the list (NOT to the ContentType specifically), it seems to work fine. So it looks like the changes are somehow pushed down to the list content type instances, where they're not in my code example. I can think of two options: either push down the changes myself by adding the workflow association to all list content types as well, or find the way SharePoint does that same thing and call it. I would prefer that last option, but untill now I didn't find a method which seems to do this.

Update 2: I now copied the SPWorkflowAssociation object down to the list content types, and still nothing :S If I check the list configuration, they're listed under the contenttypes and the settings are set to allow manual starting (even explicitely did this in code), but once more; the flows aren't visible on the workflow page for an item.

2
  • So originally you are tried to associate workflow to Content Type not Document Library? Commented Sep 16, 2011 at 11:34
  • @Vedran Rasol: I've tried both adding the workflow to the content type as well as the document library, both fail.
    – Jasper
    Commented Sep 20, 2011 at 9:14

5 Answers 5

2

Ok I finally found it. Stupid error but easily overlooked. I had used the base workflow template id's for the English workflow templates, but my site was in Dutch. So apparantly, you're allowed to create workflows in the wrong language, they show up in all of your settings pages but you just can't start them. Good thing to know, huh?

1

Use this code to programmatically add workflow to a list. Check the step 2 to properly set the workflow to start manually (set SPWorkflowAssociation.PermissionsManual after AllowManual = true).

// 1. Use SPWorkflowAssociation class
SPWorkflowAssociation workflowAssociation =
   SPWorkflowAssociation.CreateListAssociation(approvalWorkflowTemplate, "Approval", taskList, historyList);

// 2. Set workflow start options
workflowAssociation.AllowManual = true;

        //manage lists permissions is required for starting a workflow                  
          if (workflowAssociation.AllowManual)
                 {
                    SPBasePermissions emptyMask = SPBasePermissions.EmptyMask;                       
                    emptyMask |= SPBasePermissions.ManageLists;                        
                    workflowAssociation.PermissionsManual = emptyMask;
                 }

    //true or false
    workflowAssociation.AutoStartChange = false|true;
    workflowAssociation.AutoStartCreate = false|true;

// 3. Set additional association options (if any)
workflowAssociation.AssociationData = associationData;

// 4. Associate workflow to the list
list.WorkflowAssociations.Add(workflowAssociation);

// 5. Enable workflow association for displaying it in the user interface
workflowAssociation.Enabled = true;
2
  • Hello Amit, I tried exactly your code. Again, the workflow is visible in the settings (List Workflows) but I'm unable to start it. The extra step you mention is optional as far as I know, it's only necessary if you want to limit the option to start a workflow to users with manage list permissions.
    – Jasper
    Commented Nov 22, 2011 at 8:56
  • Step 3 : workflowAssociation.AssociationData = associationData. Have you performed this step? You definitely have to assign 'approver group' in the "Approval workflow. If you need help on this, please check : blog.voltje.be/?p=168 Commented Nov 22, 2011 at 9:55
0

I think you need to set AllowManual to true for your workflowAssociation.

SPWorkflowAssociation workflowAssociation =
              SPWorkflowAssociation.CreateListAssociation(approvalWorkflowTemplate, "Approval", taskList, historyList);
workflowAssociation.AllowManual = true;
list.WorkflowAssociations.Add(workflowAssociation);

but then you are saying that manual option is checked (!?)

Related question on SO: Add SharePoint workflow to a list programmatically

Edited (since original question was modified):

Maybe this is the issue here:

SPContentType.UpdateWorkflowAssociationsOnChildren Method

3
  • I think that's the default, I didn't set it but it's definitely checked in the interface. I also tried going through all the config steps in the interface and saving the flow again (to force an update of all those properties), but that doesn't make it available either.
    – Jasper
    Commented Sep 14, 2011 at 14:17
  • UpdateWorkflowAssociationsOnChildren is meant to update workflow association on children of the content type, for instance child ct's of a site ct, isn't a solution for this problem. Would only be appliccable when you declare your worklow on the parent ct and want the children ct's to copy it, which is not the case.
    – Jasper
    Commented Sep 16, 2011 at 12:22
  • Then I misunderstood your question update... Commented Sep 16, 2011 at 12:25
0

In this case, please verify the AddWorkflowAssociation method that you are using. To associate a workflow to a list or document library, use the AddWorkflowAssociation method of the SPList object. To associate a workflow with a content type, use the AddWorkflowAssociation method of the SPContentType object.

If that does not help, you could also think about triggering the workflow for the item programmatically using the SPWorkflowManager object. Please check these msdn articles which could help you - http://msdn.microsoft.com/en-us/library/ms463007(v=office.12).aspx

SPWorkflow Manager - http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.workflow.spworkflowmanager_members(v=office.12).aspx

SPWorkflowManager.StartWorkflow method - http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.workflow.spworkflowmanager.startworkflow(v=office.12).aspx

1
  • I have verified that I'm using the right method, since the workflows are listed in the proper place but can't be started. Using a different approach is a workaround, but not a solution.
    – Jasper
    Commented Nov 21, 2011 at 10:39
0

Hope I'm not making things worse, but this seemed similar:

http://www.thorntontechnical.com/tag/associate-workflow

Maybe it won't apply though.

EDIT: Ah, true. I missed that it was not content type. The only other idea I have is to try "WorkflowManager.StartWorkflow" function from:

http://www.eggheadcafe.com/microsoft/SharePoint-Development/29625092/cant-start-workflow-from-aspx-page--wss30-workflow.aspx

EDIT2: I just saw that Deepu Nair already said that... and that you solved it. Good to know, Thank you for posting the solution.

1
  • That code is for adding workflow to specific content types, I want to add it to my list and make it available for all content types in the list.
    – Jasper
    Commented Nov 22, 2011 at 8:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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