I have been looking at using the new Content Organizer feature in SharePoint 2010. The idea being that when people upload or ‘Send To’ a document center, the document is routed to the correct location.
When I enable the Content Organizer feature on my site, the ‘Drop Off’ library is created.
I then create a rule to route the document to the ‘Documents’ library. I don’t want the document to be published and visible to all users until it has been approved. So I create a simple Workflow to do approval which should start when the document is created.
Unfortunately, the workflow never starts automatically once Content Organizer has been enabled.
I had a look at the Content Organizer code with Reflector, and it seems to be executing a SystemUpdate after it has moved the document to the correct location. I assume it is running this as one of the service accounts. (I have not checked which one).
When I look at the ULS logs in more detail, I see the following exception is occurring when I upload a document.
“Declarative workflows cannot automatically start if the triggering action was performed by System Account. Canceling workflow auto-start.”
I tried submitting the document as a user other than System Account, but the error persisted.
So I decided to look at starting the workflow programmatically. I created an Event Receiver is Visual Studio 2010.
I found this post from Tobias Zimmergren with some sample code to programmatically start a workflow.
http://www.zimmergren.net/archive/2009/01/25/starting-a-sharepoint-workflow-from-code-event-receiver.aspx
I overrode the ItemAdded event as follows:
public override void ItemAdded(SPItemEventProperties properties)
{
base.ItemAdded(properties);
//SPListItem item = properties.ListItem;
//item["Title"] = " " + item["Title"] + " - added " + DateTime.Now;
//item.Update();
if (properties.ListItem.ParentList.TemplateFeatureId==new Guid("00bfea71-e717-4e80-aa17-d0c71b360101"))
{
SPWorkflowManager wfManager = properties.ListItem.ParentList.ParentWeb.Site.WorkflowManager;
SPWorkflowAssociationCollection wfassociationCollection = properties.ListItem.ParentList.WorkflowAssociations;
foreach (SPWorkflowAssociation wfAssociation in wfassociationCollection)
{
if (wfAssociation.BaseId == new Guid("8ad4d8f0-93a7-4941-9657-cf3706f00409"))
{
wfManager.StartWorkflow(properties.ListItem, wfAssociation, wfAssociation.AssociationData, true);
break;
}
}
}
}
I got the baseid for the WorkflowAssociation using SharePoint Manager:
I run the event receiver by pressing F5 in Visual Studio and watch the behaviour.
When I upload the document, the content organizer routes the document to the folder in the library and the workflow starts.
I expect to spend some more time refining the code for the eventreceiver to make sure it only executes under the right circumstances, but thought I would share in case anyone else is experiencing the same issue.