Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have a problem with Content Types... I have houndreds of lists that has some content Type and a configuration list in which I store the content type to be attached to my custom list type.

I have an event handler attached to my configuration list so that if I add or Remove a row (with the name of the Content Type) the content type is removed from every list that is of my custom type.

Everything works fine but if I remove a CT from a list programmatically remains the possibility to create a new item of that content type on my custom list!

Let's see an example:

  • Add row to the list (CT named Foo) -> Content Type added to every custom list -> Add an item in my list of the Foo CT
  • Remove row Foo from the config list -> Content Type is removed from every custom list

Now, the list has no more the CT visible in the settings but if I tried to add a new Item I see also the Foo type!

Any suggestion?

The deletion is done by this code:

lists[i].ContentTypes.Delete(lists[i].ContentTypes[properties.ListItem["Title"].ToString()].Id);
lists[i]-Update();

Thank you

share|improve this question

1 Answer 1

up vote 1 down vote accepted

The problem presents when the List has some elements inside. I solved the problem notifying to the users the files to "remove or convert" before the removal of the content type.

To notify where the problem is, my Event Receiver on deleting looks like this:

 string messaggio = web.Lists.Cast<SPList>().Where(lista => lista.BaseTemplate ==UtilsSavio.GetTypeFaldone(web)).Aggregate(String.Empty, (current1, lista) => lista.Items.Cast<SPListItem>().Where(item => item.ContentTypeId.IsChildOf(web.ContentTypes["Nuovo"].Id)).Aggregate(current1, (current, item) => current + item.Web.Url + "/" + item.Url + " <br />"));
       if (!String.IsNullOrEmpty(messaggio))
       {
           properties.ErrorMessage ="Cannot remove Content Type until these items are removed or converted: <br />" + messaggio;
           properties.Cancel = true;
       }
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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