Content Organiser and Workflow Fix

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.

References

http://sharepoint-sandbox.com/index.php?/SharePoint-2010/Content-Management/tip-88-sharepoint-2010-content-organizer-execution-order.html?directory=14

http://blog.ozippy.com/2010/06/starting-workflow-when-content.html

Advertisements

Content Organiser and Workflow Bug In SharePoint 2010

SharePoint 2010 introduced a new feature “Content Organizer” to organize your content automatically, based on custom-defined rules. However, these rules add new entity to the hierarchy of events that can be executed for the List items, and this execution order is not well documented. It’s important to understand the sequence to process documents accordingly.

For example, when the user uploads his CV to the library you want to add watermark by Workflow, update .docx metadata with event receiver and round document to “unprocessed” folder using content organizer. We have “DropOff Library” with WorkFlow, EventReceiver attached and with Content Rules. The default execution sequence will be the following:

  1. WorkFlow
  2. Event-Receiver
  3. Content Organizer Routing Rules

PS: Take into account that WF/EventReceiver sequence can be changed programmaticaly, using Sequence property of SPList.EventReceivers, but when you reconfigure Workflow it always gets the first order in execution

PPS: Content Organizer Routing Rules impersonates the Application Pool account, thus in might perform actions under “System Account” and your destination folder’s WorkFlow will not start.