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.
Code
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.RemoveAt(j);
cTypes.Insert(0, cType);
root.UniqueContentTypeOrder = cTypes;
root.Update();
}
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 comment