2

NOTE! This post is about adding MANY, BUT NOT ALL content types to your rule. If you simply need to create a rule for ALL content types, go straight to the bottom of this post.

I'm creating a Powershell script to automatically add a lot of content organizer rules to a Sharepoint site.

The site has loads of content types, and each rule is valid for all the content types. Therefore I want to add all content types to each rule's Content Type Aliases property (the "RoutingAliases" field in the Content Organizer Rules list).

Here is the code to create a content organizer rule:

$Web = Get-SPWeb "http://sharepoint/docs"
$Rule = New-Object Microsoft.Office.RecordsManagement.RecordsRepository.EcmDocumentRouterRule($Web)
$Rule.Name = "Test rule"
$Rule.ConditionsString = "<Conditions></Conditions>"
$Rule.ContentTypeString = "General document"
$Rule.Priority = "5"
$Rule.RouteToExternalLocation = $false
$Rule.TargetPath = "/docs/department"
$Rule.Enabled = $true

$Aliases = "Letter/Letter from CEO/Letter from CFO"
$Rule.Aliases = $Aliases

$Rule.Update()

The code above only works as long as $Aliases is less than 255 characters long.

Update

I haven't found any way to get around the 255 character limit to the RoutingAliases field.

The only solution I have found is to create several rules. I have a check in my script, so when I reach 255 characters I create a new rule. See example below:

$Web = Get-SPWeb "http://sharepoint/docs"
$Rule = New-Object `
    Microsoft.Office.RecordsManagement.RecordsRepository.EcmDocumentRouterRule($Web)
$Rule.ConditionsString = `
    "<Conditions></Conditions>"
$Rule.ContentTypeString = "General document"
$Rule.Priority = "5"
$Rule.RouteToExternalLocation = $false
$Rule.TargetPath = "/docs/departmentx"
$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 $ContentTypesConfigFile)
$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()

This script will create "Rule 1", "Rule 2", etc, untill all document types are added as aliases to a rule.

Another update

The script above is necessary if you need to add many, but not all content types to your rule. However, if you want to create a rule for ALL content types, simply set

$Rule.Aliases = "*"

This rule will catch all content types.

2
  • 1
    It's not because there are 12, it's the maximum text length of the field in characters.
    – Hugh Wood
    Dec 4, 2012 at 15:43
  • $Rule.Aliases can be max 255 characters long. I updated the text above to reflect this. Thanks, @HughWood Dec 11, 2012 at 12:49

1 Answer 1

0

Could you try adding them one at a time? For instance:

$Aliases = @( "Letter", "Letter from CEO", "Letter from CFO" )  # add more aliases here as needed

for ($i = 0; $i -lt $Aliases.Length; $i++)
{
    $Rule.Aliases += $Aliases[$i]
    if ($i -lt $Aliases.Length - 1) { $Rule.Aliases += "/" }
}

You might need an update in there as well.

That might avoid reaching some text limit as implied by Hugh Wood.

6
  • This didn't work, I'm afraid. I got the same error as when I ran my own code. I tried putting a $Rule.Update() in the loop as well, but a new rule was created every time I called Update(). Dec 6, 2012 at 9:52
  • @DylanCristy: Is there a way to create content organizer rule using csom powershell for SharePoint 2013 Online?
    – Asad Refai
    Jul 23, 2015 at 13:32
  • @AsadRefai I am not familiar with the Powershell cmdlets available for SP Online, so I couldn't really say, sorry. Jul 23, 2015 at 13:44
  • @DylanCristy : So is it possible using c# csom?
    – Asad Refai
    Jul 23, 2015 at 13:51
  • @AsadRefai, again, not really sure, sorry. Jul 23, 2015 at 13:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.