How to Create Content Organizer Rules Programmatically using C# Client Object Model in SharePoint Office 365

Sathish Nadarajan
 
Solution Architect
March 2, 2017
 
Rate this article
 
Views
2777

In the last articles, we saw how to create the content Organizer Rules manually. But, in case, if we want to create more number of Rules, it is not practically possible. Let us see how to create the Content Organizer Rules Programmatically.

The below method explains how to Retrieve and Create the Content Organizer Rules. The same can be used Update and Delete as well.

 namespace Console.Office365
 {
     using Microsoft.SharePoint.Client;
     using Microsoft.SharePoint.Client.Taxonomy;
     using Newtonsoft.Json.Linq;
     using System;
     using System.Collections.Generic;
     using System.IO;
     using System.Linq;
     using System.Threading.Tasks;
 
     class Program
     {
         static void Main(string[] args)
         {
             CreateContentOrganizerRules();
 
             GetContentOrganizerRules();
         }
 
         public static void GetContentOrganizerRules()
         {
             OfficeDevPnP.Core.AuthenticationManager authMgr = new OfficeDevPnP.Core.AuthenticationManager();
 
             string siteUrl = "https://**********.sharepoint.com/sites/RecordCentre";
             string userName = "Sathish@*******.onmicrosoft.com";
             string password = "***********";
 
 
             using (var clientContext = authMgr.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))
             {
                 Web web = clientContext.Web;
                 List routingRulesList = web.Lists.GetByTitle("Content Organizer Rules");
                 ListItem itm = routingRulesList.GetItemById(1);
                 clientContext.Load(web);
                 clientContext.Load(routingRulesList);
                 clientContext.Load(itm);
                 clientContext.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();
             }
         }
         public static void CreateContentOrganizerRules()
         {
             OfficeDevPnP.Core.AuthenticationManager authMgr = new OfficeDevPnP.Core.AuthenticationManager();
 
             string siteUrl = "https://******.sharepoint.com/sites/RecordCentre";
             string userName = "Sathish@********.onmicrosoft.com";
             string password = "******";
 
 
             using (var clientContext = authMgr.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))
             {
                 Web web = clientContext.Web;
                 clientContext.Load(web);
                 clientContext.Load(web.Lists);
                 clientContext.ExecuteQueryRetry();
 
                 List routingRulesList = web.Lists.GetByTitle("Content Organizer Rules");
                 clientContext.Load(routingRulesList);
                 clientContext.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();
                 clientContext.ExecuteQuery();
                 Console.WriteLine("Rule created successfully");
                 Console.Read();
             }
         }
 
 
  
     }
 }
 

Happy Coding,

Sathish Nadarajan.

Author Info

Sathish Nadarajan
 
Solution Architect
 
Rate this article
 
Sathish is a Microsoft MVP for SharePoint (Office Servers and Services) having 15+ years of experience in Microsoft Technologies. He holds a Masters Degree in Computer Aided Design and Business ...read more
 

Leave a comment