locked
Remove Content Type (reference) from Document Library

    Question

  • Hi,

    I have a Content Type sitting in a Site Collection which is used by a Document Library sitting on a web site in this Site Collection. Now, I need, by code, to remove this Content Type from the Document Library (without deleting it from the Site Collection). So, I suppose remove the reference to it. I have tried, but I'm getting a error saying that the CT is in use.
    The code:
    docLib.ContentTypes["Movie"].Delete();
    docLib.Update();

    Any ideas on how to do this?

    Thanks!
    Tuesday, August 04, 2009 10:37 AM

Answers

  • You will need to delete all the items that are based on this content type from the list first.
    Tuesday, August 04, 2009 10:51 AM
  • Hi,

      

    Thanks for Abdul Khalid’s helpful suggestion, yes you must delete all content type related list first.

     

    Sample like this:

     

    --------------------------------------------------------------------------------------------------------------------

    using (SPSite site = new SPSite("your site"))

                {

                    using (SPWeb _web = site.OpenWeb())

                    {

                       SPList doclist=_web.Lists["YourList"];

                       SPDocumentLibrary lib = doclist as SPDocumentLibrary;

                     

                       //Get All listitems, folder and subfolder included

                       SPQuery query = new SPQuery();

                       string QueryString = string.Empty;

         

                       QueryString = "<Where>" +

                                           "<Eq>" +

                                            "<FieldRef Name=\"ContentType\"/>" +

                                            "<Value Type=\"Text\">Movie</Value>" +

                                           "</Eq>" +

                                         "</Where>";

                       string ViewAttributes = "Scope=\"Recursive\"";

                       query.Query = QueryString;

                       query.ViewAttributes = ViewAttributes;

                       SPListItemCollection items = List.GetItems(query);

                           

                        //Using “-” for loop to delete all items

     

                       int iDeleteCount = items.Count;

                           for (int i = iDeleteCount - 1; i >= 0; i--)

                              items.Delete(i);

                      

                       doclist.ContentTypes["Announcement"].Delete();

     

                       doclist.Update();

                    }

                }

          }

    --------------------------------------------------------------------------------------------------------------------

     

    Hope this can help.

     

    Best Regards,

    -Aaron.

    Tuesday, August 11, 2009 3:58 AM

All replies

  • You will need to delete all the items that are based on this content type from the list first.
    Tuesday, August 04, 2009 10:51 AM
  • Hi,

      

    Thanks for Abdul Khalid’s helpful suggestion, yes you must delete all content type related list first.

     

    Sample like this:

     

    --------------------------------------------------------------------------------------------------------------------

    using (SPSite site = new SPSite("your site"))

                {

                    using (SPWeb _web = site.OpenWeb())

                    {

                       SPList doclist=_web.Lists["YourList"];

                       SPDocumentLibrary lib = doclist as SPDocumentLibrary;

                     

                       //Get All listitems, folder and subfolder included

                       SPQuery query = new SPQuery();

                       string QueryString = string.Empty;

         

                       QueryString = "<Where>" +

                                           "<Eq>" +

                                            "<FieldRef Name=\"ContentType\"/>" +

                                            "<Value Type=\"Text\">Movie</Value>" +

                                           "</Eq>" +

                                         "</Where>";

                       string ViewAttributes = "Scope=\"Recursive\"";

                       query.Query = QueryString;

                       query.ViewAttributes = ViewAttributes;

                       SPListItemCollection items = List.GetItems(query);

                           

                        //Using “-” for loop to delete all items

     

                       int iDeleteCount = items.Count;

                           for (int i = iDeleteCount - 1; i >= 0; i--)

                              items.Delete(i);

                      

                       doclist.ContentTypes["Announcement"].Delete();

     

                       doclist.Update();

                    }

                }

          }

    --------------------------------------------------------------------------------------------------------------------

     

    Hope this can help.

     

    Best Regards,

    -Aaron.

    Tuesday, August 11, 2009 3:58 AM