Create Content Organizer Rules using PowerShell

I just wanted to share a way to create Content Organizer rules in SharePoint 2010 using PowerShell. I was looking for a way to implement those rules using a script.

The first thing I did was create a rule using the UI and then fire up SharePoint Manager to have a look at it and how it has been built up.

So, those rules are stored in a hidden list called “Content Organizer Rules”. Creating a new listitem in that list however is not that straightforward. I did found out that a rule is actually an instance of the Microsoft.Office.RecordsManagement.RecordsRepository.EcmDocumentRouterRule class. The link above contains an example on how to create a rule programmatically.

If it’s possible in C#, then it must be possible in PowerShell.
The script below is what I came up with and it works like a charm:

$site = Get-SPSite
$web = Get-SPWeb
$ct = $site.RootWeb.ContentTypes["MyContentType"]
$rule = New-Object Microsoft.Office.RecordsManagement.RecordsRepository.EcmDocumentRouterRule($web)

$rule.Aliases = $ct.Name
$rule.ConditionsString = ""
$rule.CustomRouter = ""
$rule.Name = $ct.Name
$rule.Description = "Routes '" + $ct.Name + "' documents to it's own library"
$rule.ContentTypeString = $ct.Name
$rule.RouteToExternalLocation = $false
$rule.Priority = "5"
$rule.TargetPath = $web.Lists["MyLibrary"].RootFolder.ServerRelativeUrl
$rule.Enabled = $true

The thing to remember is the ConditionsString property. If you don’t specify a specific condition, you can’t just leave it empty. You have to include the tags of the Conditions element. If you do need to specify additional conditions, then the easiest way to construct this string is to create the rule in the UI, inspect the Xml string of the rule with SharePoint Manager and look at the “ows_RoutingConditions” argument, which contains the condition.

By Bart

Bart is a certified SharePoint consultant / architect at CTG Belgium NV with a broad professional experience in IT, a background in software development with a specialisation in Microsoft products and technologies and a solid knowledge and experience in Microsoft SharePoint Products and Technologies. He started as a COBOL developer on a mainframe environment and grew into software development for Windows platforms. Participated in projects varying from migrations of existing applications to development of Web applications and Windows applications. Became fascinated by the SharePoint 2007 platform and strongly believed in the added business value of this platform. Is since then fully committed to SharePoint and focuses on SharePoint implementations, migrations, integrations, design and coaching. Stays on top of new developments within the SharePoint technology stack and related technologies.

%d bloggers like this: