Delete All Content Types From List in SharePoint using CSOM

The example in this topic show how to use CSOM to Delete All Content Types From List in SharePoint

  • using Microsoft.SharePoint.Client;
  • using System.Linq;
  •  
  • using (ClientContext clientContext = new ClientContext("http://MyServer/sites/MySiteCollection"))
  • {
  • // clientcontext.Web.Lists.GetById - This option also can be used to get the list using List GUID
  • // This value is NOT List internal name
  • List targetList = clientContext.Web.Lists.GetByTitle("List Name");
  •  
  • // Load List conntent types
  • clientContext.Load(targetList, t => t.ContentTypes, t => t.ContentTypes.Include(s => s.ReadOnly));
  • clientContext.ExecuteQuery();
  •  
  • // Get list content types
  • ContentTypeCollection contentTypeCollection = targetList.ContentTypes;
  • var contentTypeCount = contentTypeCollection.Count;
  • int j = 0;
  • for (int i = 0; i < contentTypeCount; i++)
  • {
  • ContentType ct = targetList.ContentTypes[j];
  •  
  • // Write the content type name here that you don't want to delete
  • if (!ct.Name.ToUpperInvariant().Equals("FOLDER"))
  • {
  • if (ct.ReadOnly)
  • {
  • ct.ReadOnly = false;
  • }
  •  
  • ct.DeleteObject();
  • }
  • else
  • {
  • j = 1;
  • }
  • }
  •  
  • targetList.Update();
  • clientContext.ExecuteQuery();
  • }
  •  

Thank you for reading this article. This code was tested in SharePoint 2013


© 2019 Code SharePoint