locked
How to start a workflow programmatically that was created in Sharepoint designer 2010? RRS feed

  • Question

  • Trying to start a workflow from code.  The workflow itself has been created in SPD 2010, which from what I understand, means that there is no assembly until the first time it's ran.  However, when I try to find the association record, that doesnt exist.  Is this becuase the 'code' hasnt been compiled yet?  If so, how can I have sharepoint start the just-in-time compiler for a declarative based workflow, so that can kick it off with in code?

    tia

    Rocky Sanders


    Rocky Sanders
    Friday, September 3, 2010 4:14 AM

Answers

  • Hey Rocky,

    after creating a workflow you should be able to access and associate the workflow like this:

    SPWorkflowTemplate myTemplate = currentWeb.WorkflowTemplates.GetTemplateByName("WorkflowName", System.Globalization.CultureInfo.CurrentCulture);
    
    SPList taskList = currentWeb.Lists["TaskListName"];
    SPList historieList = currentWeb.Lists["HistorieListName"];
    
    SPWorkflowAssociation myAssociation = SPWorkflowAssociation.CreateListContentTypeAssociation(myTemplate, myTemplate.Name, taskList, historieList);
    
    myAssociation.AllowManual = true or false;
    myAssociation.AutoStartChange = true or false;
    myAssociation.AutoStartCreate = true or false;
    
    myAssociation.AssociationData = string.Empty;
    
    myAssociation = myList.WorkflowAsssociations.Add(myAssociation);
    myList.Update();
    
    //here you are actually starting the workflow, if it should only start if a new item has been added you should just the property right.
    currentSite.WorkflowManager.StartWorkflow(item, myAssociation, myAssociation.AssociationData);
    
    I hope this helps. You don't need the last line of code if you don't want to start it right away.

    • Marked as answer by Wayne Fan Monday, September 6, 2010 3:28 AM
    Friday, September 3, 2010 2:02 PM

All replies

  • This example is a little dated, but maybe it will help: http://wiki.threewill.com/display/enterprise/Starting+Workflows+from+SharePoint+Events
    Rob Wilson - MCT. MCTS, MCITP SharePoint. http://sharepointblog.kellerschroeder.com http://www.therobman.com/blog
    Friday, September 3, 2010 5:05 AM
  • Rob,

    thanks.  But when I tried to look through the assocation collection, I dont see any thing for my specific list/workflow.  I've manually created the workflow using standard sp user interface (i.e. list/workflow create...), and it does appear on the list as an available workflow.  but nothing in the association that I can see (stepping through the object in the debugger).  So, do I need to associate the workflow with the list in code? or am I looking at the wrong location for the association?

    thx

    rs


    Rocky Sanders
    Friday, September 3, 2010 5:11 AM
  • If you created it as a list workflow in SPD (you may even want to check the box that allows users to start it manually), then publish, I think the association is done at that point.  I would need to confirm that....won't be able to until this evening.


    Rob Wilson - MCT. MCTS, MCITP SharePoint. http://sharepointblog.kellerschroeder.com http://www.therobman.com/blog
    Friday, September 3, 2010 12:38 PM
  • Hey Rocky,

    after creating a workflow you should be able to access and associate the workflow like this:

    SPWorkflowTemplate myTemplate = currentWeb.WorkflowTemplates.GetTemplateByName("WorkflowName", System.Globalization.CultureInfo.CurrentCulture);
    
    SPList taskList = currentWeb.Lists["TaskListName"];
    SPList historieList = currentWeb.Lists["HistorieListName"];
    
    SPWorkflowAssociation myAssociation = SPWorkflowAssociation.CreateListContentTypeAssociation(myTemplate, myTemplate.Name, taskList, historieList);
    
    myAssociation.AllowManual = true or false;
    myAssociation.AutoStartChange = true or false;
    myAssociation.AutoStartCreate = true or false;
    
    myAssociation.AssociationData = string.Empty;
    
    myAssociation = myList.WorkflowAsssociations.Add(myAssociation);
    myList.Update();
    
    //here you are actually starting the workflow, if it should only start if a new item has been added you should just the property right.
    currentSite.WorkflowManager.StartWorkflow(item, myAssociation, myAssociation.AssociationData);
    
    I hope this helps. You don't need the last line of code if you don't want to start it right away.

    • Marked as answer by Wayne Fan Monday, September 6, 2010 3:28 AM
    Friday, September 3, 2010 2:02 PM
  • You should check your workflow in SPD and ensure that workflow is configured to start manually.

    I think following links help you to start workflow programatically

    http://unclepaul84.blogspot.com/2009/12/starting-sharepoint-2010-site-workflows.html

    http://www.tonytestasworld.com/post/Howto-Start-a-Sharepoint-Workflow-Programmatically.aspx

    http://www.sharepointkings.com/2008/09/how-to-start-workflow-programmatically.html

    I hope above links help you somehow.

    Friday, September 3, 2010 2:29 PM
  • Andreas.

    thank you very much.  It basically worked!  It looks that when a sharepoint designer workflow is published, the first time you want to use it, have to call this association routine (even if I had already associated it manually with the same list, the code didnt see it in the association;)

    However, as I found out, once you have associated it, it's now there, so on the 2nd,nth times, if you try to run this code, it errors out.  so need a if statement in the code to determine if to call the association or not.

    Now, next problem is to inject the inititiation data.  I saw from http://wiki.threewill.com/display/enterprise/Starting+Workflows+from+SharePoint+Events how the author was doing a data load for the association data.  However, it looks like the actual schema/format is different in sharepoint 2010 than 2007.  does anybody know where the file/schema definitions are stored for a sharepoint designer initiation/association form are kept?  And/or where in microsofts documentation set are these discussed?

    plugging away 1 issue at a time. 

    !!!

    thanks everybody so far!

    rs


    Rocky Sanders
    Friday, September 3, 2010 9:12 PM
  • Hi Rocky,

    Thanks for your post.

    I think it's a new issue, so you'd better create a new thread instead of asking many questions in the one thread, thanks.

    Monday, September 6, 2010 3:31 AM