Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Create content organizer rule using csom PowerShell
# replace these details (also consider using Get-Credential to enter password securely as script runs)..
$username = "SomeOne@SharePoint.onmicrosoft.com"
$password = "YourPassword"
$url = "https://YourSite.sharepoint.com"
$securePassword = ConvertTo-SecureString $Password -AsPlainText -Force
# the path here may need to change if you used e.g. C:\Lib..
Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
# connect/authenticate to SharePoint Online and get ClientContext object..
$clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)
$clientContext.Credentials = $credentials
if (!$clientContext.ServerObjectIsNull.Value)
{
Write-Host "Connected to SharePoint Online site: '$Url'" -ForegroundColor Green
$web = $clientContext.Site.RootWeb
$listRoutingRules = $web.Lists.GetByTitle("Content Organizer Rules")#RoutingRules
$clientContext.Load($listRoutingRules)
$clientContext.ExecuteQuery()
#Add an item to the list
$ListItemInfo = New-Object Microsoft.SharePoint.Client.ListItemCreationInformation
$Item1 = $listRoutingRules.AddItem($ListItemInfo)
$Item1["Title"] = "Asad Test Rule"
$Item1["RoutingRuleName"] = "Asad Test Rule"
$Item1["RoutingRuleDescription"] = "Asad Test Rule"
$Item1["RoutingPriority"] = 1
$Item1["RoutingEnabled"] = $true
$Item1["RoutingContentType"] = "Your Content Type Name"
$Item1["RoutingContentTypeInternal"] = "0x000000000000000000000000000000000000000000000000000000|Your Content Type Name"
$Item1["RoutingConditions"] = '<Conditions><Condition Column="xxxx-xxx-xxx-xxx-xxxxxx|ColumnName|Column Name" Operator="EqualsOrIsAChildOf" Value="1;#Value|xxxx-xxx-xxx-xxx-xxxxxx" /></Conditions>'
$Item1["RoutingConditionProperties"] = "Condition Property"
$Item1["RoutingAliases"] = "Alias Name"
$Item1["RoutingTargetLibrary"] = "Target Library"
$Item1["RoutingTargetFolder"] = ""
$Item1["RoutingTargetPath"] = "/sites/YourSite/Target Library"
$Item1["RoutingAutoFolderProp"] = "Folder Property"
$Item1["RoutingAutoFolderSettings"] = '<AutoFolder><Properties><Property Name="AutoFolderEnabled" Value="True" /><Property Name="AutoFolderPropertyName" Value="Your Value" /><Property Name="AutoFolderPropertyInternalName" Value="YourValue" /><Property Name="AutoFolderPropertyID" Value="xxxx-xxx-xxx-xx-xxxxx" /><Property Name="AutoFolderPropertyFormat" Value="%1 - %2" /><Property Name="AutoFolderPropertyTypeAsString" Value="TaxonomyFieldType" /><Property Name="AutoFolderPropertyTermStore" Value="xxxx-xxx-xxx-xxx-xxxxxx" /></Properties></AutoFolder>'
$Item1["RoutingCustomRouter"] = ""
$Item1["RoutingRuleExternal"] = $false
$Item1.Update()
$clientContext.ExecuteQuery()
Write-Host "Rule created successfully" -ForegroundColor Green
}
@pratk30
Copy link

pratk30 commented Apr 17, 2016

Could you please explain below line where we are using term value :
$Item1["RoutingConditions"] = ''

I need to know about term value format here :
1;#Value|xxxx-xxx-xxx-xxx-xxxxxx

@asadrefai
Copy link
Author

asadrefai commented Sep 16, 2016

Hi @pratk30

Sorry to reply so very late. I am not sure why I did not get the email notification for it.

So if you see the sample value which I described in the function

<Conditions><Condition Column="xxxx-xxx-xxx-xxx-xxxxxx|ColumnName|Column Name" Operator="EqualsOrIsAChildOf" Value="1;#Value|xxxx-xxx-xxx-xxx-xxxxxx" /></Conditions>

Column as described below:

xxxx-xxx-xxx-xxx-xxxxxx|ColumnName|Column Name ==  ID of column|InternalNameOfColumn|Display Name Of Column

Value as described below:

My values were of Type Metadata, coming straight from the term store

Value="1;#Value|xxxx-xxx-xxx-xxx-xxxxxx"
  • 1: This value carries no importance. When you are creating new Rule just use 1. What I have observed, that as and when you update this gets incremented.
  • ;#Value: Here Value is nothing but the Term Name
  • xxxx-xxx-xxx-xxx-xxxxxx: Its the ID of the Term

If incase we have some text values, you simply add plain text i.e. Value=Hello World

@benstegink
Copy link

benstegink commented Mar 24, 2017

I've tried to implement this, and while it adds the content organizer rules they don't actually work. All the rules show in the list, but if I add documents to the drop-off library they aren't routed. To get it to work, I need to go to the Content Organizer Rules, open the rule and click OK (I don't change anything). Now if I upload a document it works as I would expect. If I compare the list properties both before I click OK and after I click OK, they are exactly the same with the exception of the modified date. Have you seen this before or have any insight on what else might be happening behind the scenes when the OK button is clicked?

@asadrefai
Copy link
Author

asadrefai commented May 5, 2017

@benstegink No I haven't experienced this. But thanks for bringing this up, I will check if anything like that occurs.

@Terzenta
Copy link

Terzenta commented Nov 3, 2017

When I use this on Sharepoint online/365 the finding script doesn't find the rules I have set up using the GUI. If I run the script to create rules it will create one, but it doesn't show up in the content organizer rules list for the GUI. It seems like it is creating them in a different location than the rules in the GUI.

@JeanNetryValere
Copy link

JeanNetryValere commented Dec 4, 2017

Same behavior than @benstegink even if completing the provisioning by setting the linked property bags. Is there any additional task to achieve to get it work ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment