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”
2. Have created two content types CT1, CT2.
3. Now add these two content types to the List. Am doing this manually.
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.
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
These are very small code snippets, but are very useful during the creation of lists/libraries.
Happy Coding,
Sathish Nadarajan.
Leave a comment