Programmatically set a content type as default content type in a SharePoint 2013 List

Use the bellow code to set a content type as a default content type for a SharePoint List. This code simply re-orders the sequence of content type by removing it the from the current index and inserting it again as the first element. This code does not removes or re-attaches the content type from the list, it simply changes the order of content type sequence.

private void SetDefaultContentType(SPList list, string ContenTypeName)
     IList<SPContentType> cTypes = new List<SPContentType>();
     SPFolder root = list.RootFolder;
     cTypes = root.ContentTypeOrder;
     SPContentType cType = cTypes.SingleOrDefault(hd => hd.Name == ContenTypeName);
     int j = cTypes.IndexOf(cType);
     cTypes.Insert(0, cType);
     root.UniqueContentTypeOrder = cTypes;
How to Use:
using (SPSite site = new SPSite("[Server URL]"))
     using (SPWeb web = site.OpenWeb())
         SPList list = web.Lists["Your List Name"];
         SetDefaultContentType(list, "Your Content Type Name");

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.