How to Re-Order Content Types in SharePoint Office 365 Programmatically using C# Patterns and Practices PNP

Sathish Nadarajan
 
Solution Architect
September 16, 2017
 
Rate this article
 
Views
2956

Sometime back, we saw how to add content types to a List/Library. I met with a requirement to re-order the Content Type as well. Let me explain in detail with the screen shots.

1. I have a List called “Test List”

clip_image002

2. Have created two content types CT1, CT2.

clip_image004

3. Now add these two content types to the List. Am doing this manually.

clip_image006

4. After adding these two content types, if I go to the List settings, I can see three content types. Item, CT1 & CT2.

5. When I click on the New Item, I could see these content types in a specific order.

clip_image008

6. I want this to be in a different order. i.e., Item, CT2 & CT1.

7. We can do this using the PNP.

8. The code snippet will be as below.

 using Microsoft.SharePoint.Client;
 using System;
 
 namespace Office365.Console
 {
     class Program
     {
         static void Main(string[] args)
         {
             ReOrderContentType();
         }
 
         public static void ReOrderContentType()
         {
             OfficeDevPnP.Core.AuthenticationManager authMgr = new OfficeDevPnP.Core.AuthenticationManager();
 
 
 
             string siteUrl = "https://****.sharepoint.com/sites/DeveloperSite/";
             //string siteUrl = item.TargetSiteUrl;
             string userName = "sathish@****.onmicrosoft.com";
             string password = "********";
 
             
             using (var clientContext = authMgr.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))
             {
                 try
                 {
                     Web web = clientContext.Web;
                     
                     clientContext.Load(web);
                     clientContext.Load(web.Lists);
                     clientContext.ExecuteQuery();
 
                     List list = web.Lists.GetByTitle("Test List");
                     clientContext.Load(list);
                     clientContext.Load(list.ContentTypes);
                     clientContext.ExecuteQuery();
 
                     list.ReorderContentTypes(new string[] { "Item", "CT2", "CT1" });
                     list.Update();
                     clientContext.ExecuteQuery();
                 }
                 catch (Exception ex)
                 {
                     System.Console.ForegroundColor = ConsoleColor.Red;
                     System.Console.WriteLine("Exception Occured : " + ex.Message);
                     System.IO.File.AppendAllText("C:\Temp\Exception.txt", ex.Message + " - " + siteUrl + Environment.NewLine);
                 }
 
 
             }
 
 
             System.Console.WriteLine("Completed....");
             System.Console.WriteLine("Press Any Key to Exit ....");
             System.Console.ReadLine();
         }
          
 
          
     }
 }
 
 

9. Once, the piece of code got executed, then the output will be

clip_image010

These are very small code snippets, but are very useful during the creation of lists/libraries.

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