1

Can we create custom content organizer rules using Client Object Model in SharePoint 2013? If no, then what way of doing it should be followed?

  • Did you manage to find an appropriate solution for it? – Asad Refai Jul 23 '15 at 10:29
1

I had a similar requirement in SharePoint Online and I have finished the Quest and you can refer it over Create content organizer rule in SharePoint Online using CSOM code. I believe you are required to use similar approach except instead of connecting to a SharePoint Online tenant site you need to get connected to the one on premise.

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)
        {


            using (ClientContext clientCtx = new ClientContext("http://yoursite.com"))
            {
                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:

# 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 and get ClientContext object.. 
$clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($url) 
$credentials = Get-Credential 
$clientContext.Credentials = $credentials 

if (!$clientContext.ServerObjectIsNull.Value) 
{ 
    Write-Host "Connected to SharePoint site" -ForegroundColor Green 

    $web = $clientContext.Web
    $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)
        {
            using (ClientContext clientCtx = new ClientContext("http://yoursite.com"))
            {
                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:

# 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 and get ClientContext object.. 
$clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($url) 
$credentials = Get-Credential 
$clientContext.Credentials = $credentials 

if (!$clientContext.ServerObjectIsNull.Value) 
{ 
    Write-Host "Connected to SharePoint site" -ForegroundColor Green 

    $web = $clientContext.Web
    $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
} 
0

hope this blog will help you. It for SharePoint 2010, but it will get you to right direction.

  1. SharePoint 2010 Content Organizer - Client Object Model

    SharePoint Server 2013 Client Components SDK

  2. And this: Retrieve Managed Metadata using JavaScript and SPServices

  • Unfortunately given reference links do not contain any explanation on how to create a custom content organizer rules using Client Object Model – Asad Refai Jul 23 '15 at 10:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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