SharePoint: Create a content organizer rule with many content type aliases using PowerShell

PowerShell script for creating a content organizer rule

Why would I want to add many content type aliases to a content organizer rule? When creating a content organizer rule in SharePoint, the default is that the rule is only valid for a single content type. To get around this limitation, you can add other content type names to the field Aliases (also known as RoutingAliases), displayed as “List of alternate names” in the SharePoint editor.

Published:

Warning 1: If you want to add all content types, you simply need to add “*” (wildcard) as an alias. The code example below is a solution if you need many, but not all content types.

Warning 2: Using this method also changes the content type of the document being routed, so if you need to keep the content type, you need to create one rule per content type. My script below can still be useful to automatically create all the rules you need.

So, what’s the problem?

The aliases field is limited to 255 characters, which means that if you want to create one rule that will work with many content types in your solution, that is usually not possible, because the list of content types exceeds 255 characters.

How do I get around this limitation?

The suggested solution below automatically creates a new rule and adds content type names until the aliases field is full. When the field cannot hold any more content type names, the script starts creating a new rule.

The end result is one or more rules that do exactly the same operation, but for different content types, and as few rules as possible.

$web = Get-SPWeb "http://sharepoint/docs" $rule = New-Object Microsoft.Office.RecordsManagement.RecordsRepository.EcmDocumentRouterRule($web) $rule.ConditionsString = "" $rule.ContentTypeString = "General document" $rule.Priority = "5" $rule.RouteToExternalLocation = $false $rule.TargetPath = "/docs/department" $rule.Enabled = $true $aliases = "" $ruleNumber = 1 $rule.Name = "My rule " + $ruleNumber # Add all contenttypes to the new rule # NB! $rule.Aliases can only be up to 255 # characters long, when this limit is reached # we have to make a new rule # Note that I have all my content types stored # in an XML file, but you might as well get this # from SharePoint itself $contentTypes = [xml] ( Get-Content "ContentTypes.xml") $contentTypes.config.contenttypes.contenttype | ForEach-Object { if(($aliases.Length + $_.Name.Length) -lt 250) { # Keep adding document types # until we reach the limit if($aliases.Length -gt 0) { $aliases += "/" } $aliases += $_.Name } else { # Reached limit, create rule, # start working on next rule $rule.Aliases = $aliases $rule.Update() Write-Host "Rule created:" $rule.Name $ruleNumber++ $rule.Name = $ruleName + " " + $ruleNumber $aliases = $_.Name } } # Save final rule $rule.Aliases = $aliases $rule.Update()

Categories: PowerShell SharePoint

Comments