Scenario
We want to start or restart SharePoint workflow programmatically for the list item.
Solution
SPList list = ...; // initialise your list
SPWorkflowAssociation workflowAssociation = null;
workflowAssociation = list.WorkflowAssociations.GetAssociationByName("Mighty Workflow", System.Globalization.CultureInfo.CurrentCulture);
if (workflowAssociation != null)
{
foreach (SPListItem item in list.Items)
{
foreach (SPWorkflow wf in item.Workflows)
{
if (wf.AssociationId == workflowAssociation.Id)
{
// if we want to restart - cancel the running one first
if (!wf.IsCompleted)
{
try
{
SPWorkflowManager.CancelWorkflow(wf);
}
catch
{
}
}
//
try
{
// start the workflow
site.WorkflowManager.StartWorkflow(item, workflowOrderProcessAssociation, workflowOrderProcessAssociation.AssociationData, true);
}
catch
{
}
}
}
}
}
We want to start or restart SharePoint workflow programmatically for the list item.
Solution
SPList list = ...; // initialise your list
SPWorkflowAssociation workflowAssociation = null;
workflowAssociation = list.WorkflowAssociations.GetAssociationByName("Mighty Workflow", System.Globalization.CultureInfo.CurrentCulture);
if (workflowAssociation != null)
{
foreach (SPListItem item in list.Items)
{
foreach (SPWorkflow wf in item.Workflows)
{
if (wf.AssociationId == workflowAssociation.Id)
{
// if we want to restart - cancel the running one first
if (!wf.IsCompleted)
{
try
{
SPWorkflowManager.CancelWorkflow(wf);
}
catch
{
}
}
//
try
{
// start the workflow
site.WorkflowManager.StartWorkflow(item, workflowOrderProcessAssociation, workflowOrderProcessAssociation.AssociationData, true);
}
catch
{
}
}
}
}
}
Comments
Post a Comment