2

How can I remove a Content Type from a List ?

I've found code for deleting from sites but not from Lists.

2 Answers 2

6

Try this code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;

namespace CSOMSamples
{
    class Program
    {
        static void Main(string[] args)
        {
            //// String Variable to store the siteURL
            string siteURL = "http://c4968397007/";

            //// Get the context for the SharePoint Site to access the data
            ClientContext clientContext = new ClientContext(siteURL);

            //// Get the custom list by title
            List list = clientContext.Web.Lists.GetByTitle("Custom");

            //// Get the content type by id for the custom list 
            ContentType ct = list.ContentTypes.GetById("0x01003D7B5A54BF843D4381F54AB9D229F98A00823A04B0C30F5747840B1E74FB62285F");

            //// Delete the content type from the list
            ct.DeleteObject();
            clientContext.ExecuteQuery();
        }
    }
}

Source: How to delete the content type from the list using CSOM in SharePoint 2013

1

You can do it using below code

SPList list = currentWeb.Lists.TryGetList("ListName");
if (list != null)
{
    SPContentType itemCT = list.ContentTypes["CTName"];
    if (itemCT != null)
    {
        list.ContentTypes.Delete(itemCT.Id);
        list.Update();
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.