Sharpoint
Sharpoint

Reputation: 317

When do sharepoint workflows get created?

I am using sharepoint 2010 and I am stuck. I have a feature that has an FeatureAcitvated method. In this method I have a class that I want to have code that starts a workflow that I created.

So I did this

 foreach (SPListItem item in itemCollection)
     {
      SPWorkflowAssociation wfAssoc = listItem.ParentList.WorkflowAssociations[new Guid("0768433d-23b1-4797-be66-fefc486e7e08")];
     }

Yet it is always null. It never finds my workflow. When I look into WorkflowAssociations it only has a count of one of some workflow that I beleive is probably some built in one.

So I am wondering is my workflow generated at this time? Or does it get created after feature EventReceiver class gets fired?

I created my workflow as a list workflow through Visual studios 2010

Upvotes: 1

Views: 192

Answers (1)

Rob Windsor
Rob Windsor

Reputation: 6859

I think this code will solve your problem. The first thing you need to do is get the association of the workflow template to the list. This only needs to be once for the list, not once for each item. Then you loop over each item and, using the WorkflowManager property of the site collection, start the workflow on each item.

var assoc = list.WorkflowAssociations[new Guid("...")];
var manager = site.WorkflowManager;
foreach (SPListItem item in list.Items) 
{ 
    manager.StartWorkflow(item, assoc, assoc.AssociationData, true); 
}

Upvotes: 1

Related Questions