In this blog I am going to show how to change default content type programatically using the SharePoint client object model. In one of my task assignments I was using custom content type in the document library and to update this new content type as a default content type for the document library.
The top content type is always default content type for all type of SharePoint libraries. So that you have to just change the content type order of library and make your custom content type on the top.
Following is the code method to change the content type order.
Include "using SP = Microsoft.SharePoint.Client;" on the top of your class.
Where "SP.List list" is the list name and 'string contentTypeName' is the custom content type which you have to make as a default content type
The top content type is always default content type for all type of SharePoint libraries. So that you have to just change the content type order of library and make your custom content type on the top.
Following is the code method to change the content type order.
Include "using SP = Microsoft.SharePoint.Client;" on the top of your class.
void
ChangeContentTypeOrder(SP.ClientContext ctx, SP.List list, string contentTypeName)
{
SP.ContentTypeCollection
currentCTOrder = list.ContentTypes;
ctx.Load(currentCTOrder);
ctx.ExecuteQuery();
IList<SP.ContentTypeId>
reverceOrder = new List<SP.ContentTypeId>();
foreach (SP.ContentType ct in currentCTOrder)
{
if (ct.Name.Equals("contentTypeName"))
{
reverceOrder.Add(ct.Id);
}
}
list.RootFolder.UniqueContentTypeOrder = reverceOrder;
list.RootFolder.Update();
list.Update();
ctx.ExecuteQuery();
}
|
Where "SP.List list" is the list name and 'string contentTypeName' is the custom content type which you have to make as a default content type