Tuesday, September 9, 2014

Programmatically Change Default Content Type in SharePoint 2013 List


SharePoint list does not have any direct property or method to make content type default. but it is having content type ordering. thus making content type in first of the list automatically makes that content type default.

following shows a sample code

using (var site = new SPSite("url"))
   using (var web = site.AllWebs["yourweb"])
       var lst = web.Lists["Your List"];
       SPContentTypeCollection currentOrder = lst.ContentTypes;
       // make default content type
       var newOrder = currentOrder.Cast<SPContentType>().Where(ct => ct.Name.Contains("Content Type To Default")).ToList();
       lst.RootFolder.UniqueContentTypeOrder = newOrder;
Post a Comment