Sladescross's Blog

Blogging about Sharepoint related stuff

Workflow Tutorial June 4, 2012

Filed under: Sub-Workflow,Tutorial,Workflow — sladescross @ 10:14 pm

http://msdn.microsoft.com/en-us/sp2010devtrainingcourse_workflow_unit.aspx

Workflow tutorial run through from Visio to declarative.

http://msdn.microsoft.com/en-us/library/gg615452.aspx

Create and deploy a declarative workflow.

http://msdn.microsoft.com/en-us/library/ff798386.aspx

Import and package a declarative workflow.

http://blogs.msdn.com/b/edhild/archive/2011/06/01/creating-custom-workflow-task-approval-forms-with-sharepoint-designer-2010.aspx

Add fields to task form in a SPD workflow.

http://blogs.msdn.com/b/sharepointdev/archive/2011/08/23/calling-sharepoint-workflow-instances-programmatically-from-other-sharepoint-workflows.aspx

Calling a subworkflow programmatically.

However there is the Enabled property. When you set a workflow attached to a list to No New Instances, the Enabled property will be False. So with a minor tweak to our LINQ query, we can make sure that the only result returned by it is a workflow association template that both matches our base template Id and is allowed to start new instances on that particular list.

Guid svpwfid = new Guid("4e126fc9-6b12-1212-8c21-21120f264bad");
var wfa = (from SPWorkflowAssociation spwfa in workflowProperties.Item.ParentList.WorkflowAssociations
           where spwfa.BaseId == svpwfid && spwfa.Enabled == true
           select spwfa).FirstOrDefault();

Now that we have our query in place to get the proper workflow association template, we can instantiate the SPWorkflowManager object for the site, and pass it along our workflow association, the association data, our list item, and the bool flag for the auto start property through its StartWorkflow() method.

Full implementation would look like this:

Guid svpwfid = new Guid("4e126fc9-6b12-1212-8c21-21120f264bad");
var wfa = (from SPWorkflowAssociation spwfa in workflowProperties.Item.ParentList.WorkflowAssociations
           where spwfa.BaseId == svpwfid && spwfa.Enabled == true
           select spwfa).FirstOrDefault();

if (wfa != null)
{
    SPWorkflowManager wfMan = workflowProperties.Item.ParentList.ParentWeb.Site.WorkflowManager;
    wfMan.StartWorkflow(workflowProperties.Item, wfa, wfa.AssociationData, true);
}
else
{
    // could not find workflow template.
}

Summary:

And that’s how you find and start a workflow, from another workflow.

About these ads
 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

Join 63 other followers