Set List/Library Default Content Type Programatically

Leave a Comment
Hi All,

below example will demonstrates how to set the default content type to list/library programmatically in SharePoint.          


             SPList oList = oWeb.Lists.TryGetList("ListName");
                SPContentType cType = oWeb.ContentTypes["ContentTypeName"];
                if (cType != null)
                {   //enabling the content type for the list
                    oList.ContentTypesEnabled = true;
                    //adding conten type
                    oList.ContentTypes.Add(cType);

                    SPContentTypeCollection currentOrder = oList.ContentTypes;
                    List<SPContentType> result = new List<SPContentType>();
                    foreach (SPContentType ct in currentOrder)
                    {
                        if (ct.Name.Contains("ContentTypeName"))
                        {
                            result.Add(ct);
                        }
                    }

                    oList.RootFolder.UniqueContentTypeOrder = result;
                    oList.Update();

Related Post

0 comments:

Post a Comment