Question

Records Center is one of my site collection on Office 365.

I want to create some 100 odd Content organizer rule. Creating it manually does not make any sense.

So I would want to automate this process of creation.

Are there any possible options or ideas to make this automate? Also are below options valid?

  1. Have a console application and CSOM c# create rules
  2. A CSOM PowerShell script
  3. JavaScript Object Model.
  4. To develop a Provider Hosted App, and access any possible SPO web service which can do the job.
Was it helpful?

Solution

Well it took a bit of a time to figure out how I can create content organizer rule with CSOM as I dint wanna create some 100 manually on Office 365. But finally the QUEST is complete now and I have a wonderful csom c# and Powershell code to do the job.

What's the trick: If we navigate to Content Organizer Rules from site settings, We can notice that its nothing but a list and all the Rules are stored as list items. Thankfully we have CSOM library available which allows us to save a list item using either c# or PowerShell. So the trick is to create 'Rules' as an item.

What to know: Now as we know that we create Rules as an item, But the big question remains what to store and which field/columns to set so that a proper well functioned Rule can be created.

Think Out of Box: Create a Content Organizer Rule through SharePoint UI. Now since its a list we can query it and get the values of all the fields. This is how we know SharePoint use which field/column and how are the values are stored, for example how SharePoint stores the condition.

==========Solution===========

First thing first: Get the value of a Rule which is added via SharePoint UI so that we get an idea which fields to store in and what type of values it needs.

CSOM c# code to get the Item:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;


namespace RecordCentreRule
{
    class Program
    {
        static void Main(string[] args)
        {
            var targetSite = new Uri("https://YourSite.sharepoint.com");
            var login = "SomeOne@SharePoint.onmicrosoft.com";
            var password = "YourPassword";
            var securePassword = new SecureString();
            foreach (char c in password)
            {
                securePassword.AppendChar(c);
            }

            var onlineCredentials = new SharePointOnlineCredentials(login, securePassword);

            using (ClientContext clientCtx = new ClientContext(targetSite))
            {
                clientCtx.Credentials = onlineCredentials;
                Web web = clientCtx.Web;
                List routingRulesList = web.Lists.GetByTitle("Content Organizer Rules");
                ListItem itm = routingRulesList.GetItemById(1);
                clientCtx.Load(web);
                clientCtx.Load(routingRulesList);
                clientCtx.Load(itm);
                clientCtx.ExecuteQuery();


                Console.WriteLine("Title: " + itm["Title"] + "\n");
                Console.WriteLine("RoutingConditions: " + itm["RoutingConditions"] + "\n");
                Console.WriteLine("RoutingConditionProperties: " + itm["RoutingConditionProperties"] + "\n");
                Console.WriteLine("RoutingContentType: " + itm["RoutingContentType"] + "\n");
                Console.WriteLine("RoutingContentTypeInternal: " + itm["RoutingContentTypeInternal"] + "\n");
                Console.WriteLine("RoutingConditions: " + itm["RoutingConditions"] + "\n");
                Console.WriteLine("RoutingConditionProperties: " + itm["RoutingConditionProperties"] + "\n");
                Console.WriteLine("RoutingAliases: " + itm["RoutingAliases"] + "\n");
                Console.WriteLine("RoutingTargetLibrary: " + itm["RoutingTargetLibrary"] + "\n");
                Console.WriteLine("RoutingTargetFolder: " + itm["RoutingTargetFolder"] + "\n");
                Console.WriteLine("RoutingTargetPath: " + itm["RoutingTargetPath"] + "\n");
                Console.WriteLine("RoutingAutoFolderProp: " + itm["RoutingAutoFolderProp"] + "\n");
                Console.WriteLine("RoutingAutoFolderSettings: " + itm["RoutingAutoFolderSettings"] + "\n");
                Console.WriteLine("RoutingCustomRouter: " + itm["RoutingCustomRouter"] + "\n");
                Console.WriteLine("RoutingRuleExternal: " + itm["RoutingRuleExternal"] + "\n");
                Console.Read();


            }

        }
    }
}

CSOM PowerShell Code:

# 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")
    $item = $listRoutingRules.GetItemById(1)
    $clientContext.Load($listRoutingRules)
    $clientContext.Load($item)
    $clientContext.ExecuteQuery()
    Write-Host "Title: " $item["Title"]
    Write-Host "RoutingConditions: " $item["RoutingConditions"]
    Write-Host "RoutingConditionProperties: " $item["RoutingConditionProperties"]
    Write-Host "RoutingContentType: " $item["RoutingContentType"]
    Write-Host "RoutingContentTypeInternal: " $item["RoutingContentTypeInternal"] 
    Write-Host "RoutingConditions: " $item["RoutingConditions"] 
    Write-Host "RoutingConditionProperties: " $item["RoutingConditionProperties"] 
    Write-Host "RoutingAliases: " $item["RoutingAliases"] 
    Write-Host "RoutingTargetLibrary: " $item["RoutingTargetLibrary"] 
    Write-Host "RoutingTargetFolder: " $item["RoutingTargetFolder"] 
    Write-Host "RoutingTargetPath: " $item["RoutingTargetPath"] 
    Write-Host "RoutingAutoFolderProp: " $item["RoutingAutoFolderProp"] 
    Write-Host "RoutingAutoFolderSettings: " $item["RoutingAutoFolderSettings"] 
    Write-Host "RoutingCustomRouter: " $item["RoutingCustomRouter"] 
    Write-Host "RoutingRuleExternal: " $item["RoutingRuleExternal"] 

} 

So this is how we know what and How values are stored in a Rule.

Second Step would be just create a new item (Rule) with our values as inspired by above results.

CSOM c# code to create a Rule:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;


namespace RecordCentreRule
{
    class Program
    {
        static void Main(string[] args)
        {
            var targetSite = new Uri("https://YourSite.sharepoint.com");
            var login = "SomeOne@SharePoint.onmicrosoft.com";
            var password = "YourPassword";
            var securePassword = new SecureString();
            foreach (char c in password)
            {
                securePassword.AppendChar(c);
            }

            var onlineCredentials = new SharePointOnlineCredentials(login, securePassword);

            using (ClientContext clientCtx = new ClientContext(targetSite))
            {
                clientCtx.Credentials = onlineCredentials;
                Web web = clientCtx.Web;
                List routingRulesList = web.Lists.GetByTitle("Content Organizer Rules");
                clientCtx.Load(routingRulesList);
                clientCtx.ExecuteQuery();


                ListItemCreationInformation routingRuleInfo = new ListItemCreationInformation();
                ListItem routingRule = routingRulesList.AddItem(routingRuleInfo);
                routingRule["Title"] = "From Console";
                routingRule["RoutingRuleName"] = "From Console";
                routingRule["RoutingRuleDescription"] = "From Console";
                routingRule["RoutingPriority"] = 1;
                routingRule["RoutingEnabled"] = true;
                routingRule["RoutingContentType"] = "Your Content Type Name";
                routingRule["RoutingContentTypeInternal"] = "0x000000000000000000000000000000000000000000000000000000|Your Content Type Name";
                routingRule["RoutingConditions"] = "<Conditions><Condition Column=\"xxxx-xxx-xxx-xxx-xxxxx|Column|Column Name\" Operator=\"EqualsOrIsAChildOf\" Value=\"1;#WhatEver|xxxx-xxx-xxx-xxx-xxxx\" /></Conditions>";
                routingRule["RoutingConditionProperties"] = "Column Name on which you need condition";
                routingRule["RoutingAliases"] = "Your Content Type Name";
                routingRule["RoutingTargetLibrary"] = "Target Library";
                routingRule["RoutingTargetFolder"] = "";
                routingRule["RoutingTargetPath"] = "/sites/YourSite/Target Library";
                routingRule["RoutingAutoFolderProp"] = "Folder Property";
                routingRule["RoutingAutoFolderSettings"] = "<AutoFolder><Properties><Property Name=\"AutoFolderEnabled\" Value=\"True\" /><Property Name=\"AutoFolderPropertyName\" Value=\"Folder Property\" /><Property Name=\"AutoFolderPropertyInternalName\" Value=\"WhatEver\" /><Property Name=\"AutoFolderPropertyID\" Value=\"xxxx-xxxx-xxx-xxx-xxxx\" /><Property Name=\"AutoFolderPropertyFormat\" Value=\"%1 - %2\" /><Property Name=\"AutoFolderPropertyTypeAsString\" Value=\"TaxonomyFieldType\" /><Property Name=\"AutoFolderPropertyTermStore\" Value=\"xxxx-xxx-xxx-xxx-xxxxx\" /></Properties></AutoFolder>";
                routingRule["RoutingCustomRouter"] = "";
                routingRule["RoutingRuleExternal"] = false;

                routingRule.Update();
                clientCtx.ExecuteQuery();
                Console.WriteLine("Rule created successfully");
                Console.Read();

            }

        }
    }
}

CSOM PowerShell to create Rule:

# 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
} 

This is how we create rules using CSOM.

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top