0

How to delete content type from pages library. I am using below code to delete content type. But i am not able to delete and i get this message.

Specified argument was out of the range of valid values. Parameter name: id

  using (SPSite site = properties.Feature.Parent as SPSite)
            {
                using (SPWeb web = site.OpenWeb())
                {
                    web.AllowUnsafeUpdates = true;
                    SPContentType documentCType = web.AvailableContentTypes["Test"];
                    SPList list = web.Lists["Pages"];
                    list.ContentTypes.Delete(documentCType.Id);
                    web.AllowUnsafeUpdates = false;
                }
            }
1

I believe the problem you are having is due to the fact that lists maintain their own local copies of content types. You are getting the content type ID from the site level content type, but that ID does not exist at the list level. The list level content type will have an ID that is a child of the site level content type ID. So, there are two ways to go:

1) Bypass the site level entirely:

SPList list = web.Lists["Pages"];
SPContentType documentCType = list.ContentTypes["Test"];
list.ContentTypes.Delete(documentCType.Id);

2) Check the list content types for a child of the site content type:

SPContentType documentCType = web.AvailableContentTypes["Test"];
SPList list = web.Lists["Pages"];
SPContentTypeId listDocumentCTypeID = list.ContentTypes.BestMatch(documentCType.Id)
if (listDocumentCTypeID.IsChildOf(documentCType.Id))
{
    list.ContentTypes.Delete(documentCType.Id);
}

Here is more information on Site and List Content Types.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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