Friday, June 25, 2010

Starting a workflow when content organizer is enabled

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.

image 

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.

image

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:

image

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.

4 comments:

Anonymous said...

This is exactly what we have been experiencing with content organizer. Apparently, this is a known issue with Microsoft but they don't know when it will be fixed.

Thanks for the post

Anonymous said...

The workflow initiator becomes system instead of the person who drops the document in. Anyway around this?

Anonymous said...

The workflow history records that system initiated the workflow instead of the original user who dropped the document. Any way around this?

locksmith manhattan said...

Hopefully it will get fixed soon because I am having the same issues as you.

John