Sunday, 8 April 2012

A Content Type Which Does Not Belong To A Collection Cannot be Updated In Sharepoint

Hi,

An issue was reported to me where a content type was not getting created programmatically. Whenever through code columns were added to it it was throwing the exception "A Content Type Which Does Not Belong To A Collection Cannot be Updated".  I did a walk through of the code and found that through code first a new content type was instantiated and then some columns were added to this. Here is the place where the exception was originating.

I referred some articles in MSDN and found out the reason for it. The reason was that although the content type was instantiated it was not added to any collection or in other words it was not associated with any site.
Hence we added following lines of code before calling update method and it became successful.

SPWeb web = site.OpenWeb();
web.ContentTypes.Add(MyContentType);

I hope this will help you out.

Thanks,
Rahul Rashu



No comments:

Post a Comment