Creating SharePoint 2010 Content Organizer Rules programmatically

The Content Organizer (gah! Organiser!) in SharePoint 2010 is a nifty new feature that enables documents to be added to a single library (the “Drop Off Library”) and then let SharePoint route them through to the correct final resting position based on a set of configured rules. Rules can be configured per content type and allow conditions to be set based on metadata on the item. You can even use it to send documents – and document sets – between site collections, e.g., to a Records Center. Very cool.

Depending on your requirements, you may find you need to create a lot of rules which as with most things SharePoint can be cumbersome if you need to do it all through the UI. Fortunately, creating the rules programmatically is straight-forward.

A quick word of caution… – you might be inclined to think that the Content Organizer Rules list is just a list, right? So that being the case, why not just add new items to it? If you can work out the required metadata, what’s wrong with:

var listItem = rulesList.AddItem();
 
            listItem["ContentType"] = "Rule";
            listItem["Title"] = "My Rule";
            listItem["RoutingRuleName"] = "My Rule";
            listItem["RoutingPriority"] = 5;
            listItem["RoutingEnabled"] = true;
            listItem["RoutingContentType"] = contentType.Name;
            listItem["RoutingContentTypeInternal"] = string.Format("{0}|{1}", contentType.Id, contentType.Name);
            listItem["RoutingConditions"] = string.Format("<conditions><condition Column="e5c32e13-8dc3-48e8-9d98-ca66579a5634|Region|Region" Operator="IsEqual" Value="{0}"></condition></conditions>", someValue);
            listItem["RoutingAliases"] = contentType.Name;
           ... etc.
 

Well, that would, to some extent work. The rule would get created and based on my testing, the Content Organiser would honour it, to some extent.

But you don’t need to do this. Not only does crafting the bits required to get conditions right and so on get a bit hairy, but it doesn’t do a full job for you. For starters, you need to add the Content Types in use to the Drop Off Library and, in the case of some content types (e.g., Document Sets), it won’t work.

Plus, you don’t need to do it! SharePoint will handle this for you, and do all the stuff behind the scenes that needs to happen. Within the Microsoft.Office.RecordsManagement.RecordsRepository namespace (found in the Microsoft.Office.Policy assembly) is a handy class EcmDocumentRoutingRule. Your code would now look something like:

EcmDocumentRouterRule newRule = new EcmDocumentRouterRule(currentWeb);
 
            newRule.Name = "My Rule";
            newRule.Description = "My Rule";
            newRule.ContentTypeString = contentType.Name;
            newRule.RouteToExternalLocation = false;
            newRule.Priority = "5";
... etc. 
                   newRule.Update();

And that’s it. The important bit is that it does everything for you like add the Content Types to the libraries in use, and, package up the Documents as required.

See here for a useful code sample.

Be the first to comment

Leave a Reply

Your email address will not be published.


*