Responsive Ads Here

Wednesday, November 12, 2014

How to programmatically set a content type as default content type in a SharePoint 2013 List


In my recent project i have a requirement to change the order of content type of list programmatically.

 I have an announcement list which has default content type announcements,I want to add my custom content type to that list and i need to change the order of content type i.e when we are adding a new item to the list,my custom content type columns should come.

Use the below code snippet to change to set your custom content type as default content type by using server and client object model. 

Using Server Object Model :-- 

Code: 
 
private void SetDefaultContentType(SPList list, string ContentTypeName)
{
    try
    {
        IList cTypes = new List();
        SPFolder root = list.RootFolder;
        cTypes = root.ContentTypeOrder;
        SPContentType cType = cTypes.SingleOrDefault(hd => hd.Name == ContentTypeName);
        int j = cTypes.IndexOf(cType);
        cTypes.RemoveAt(j);
        cTypes.Insert(0, cType);
        root.UniqueContentTypeOrder = cTypes;
        root.Update();
    }
    catch (Exception)
    {
       throw;
    }
}

Usage :-- 

 
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");
  
     }
 }

Using Client Object Model :-- 

 
public static void SetDefaultContentType(ClientContext clientContext, string ContentTypeName)
        {
            try
            {
                List list = clientContext.Web.Lists.GetByTitle("Announcements");
                if (list != null)
                {
                    ContentTypeCollection listContentTypes = list.ContentTypes;
                    //clientContext.Load(listContentTypes, listCTs => listCTs.Include(ls => ls.Name).Where(ls => ls.Name == ContentTypeName));
                    clientContext.Load(listContentTypes);
                    clientContext.ExecuteQuery();
                    if (listContentTypes.Count != 0)
                    {
                        IList cTypes = new List();
                        foreach (ContentType ct in listContentTypes)
                        {
                            if (ct.Name != "Folder")
                            {
                                if (ct.Name == ContentTypeName)
                                {                                   
                                    cTypes.Insert(0, ct.Id);
                                }

                                else
                                {
                                    cTypes.Add(ct.Id);
                                }
                            }
                        }

                        list.RootFolder.UniqueContentTypeOrder = cTypes;
                        list.RootFolder.Update();
                        list.Update();
                        clientContext.ExecuteQuery();
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

No comments:

Post a Comment