Programmatically set a content type as default content type in a SharePoint 2013 List
Programmatically set default content type SharePoint list
Programmatically set a content type as default content type in a SharePoint 2013 List. In this post, I described to Programmatically set default content type SharePoint list. Also, I described in this post how to delete a content type. If you need to create content type programmatically follow my previous post. Create Content Type using CSOM. and Add Content Type to List or Document Library Programmatically SharePoint 2013.
[AdSense-A]
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.RemoveAt(j); cTypes.Insert(0, cType); root.UniqueContentTypeOrder = cTypes; root.Update();
Remove a content type from SharePoint List or Document Library Programmatically
SPFolder rootFolder = list.RootFolder; IList<SPContentType> ctList = rootFolder.ContentTypeOrder; foreach (SPContentType ct in ctList) { if (ct.Name == "Document") { ctList.Remove(ct); break; } } rootFolder.UniqueContentTypeOrder = ctList; rootFolder.Update();