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 http://www.mycompany.com
$web = Get-SPWeb http://www.mycompany.com
$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.