开发者

Exception when programmatically copying content type

开发者 https://www.devze.com 2022-12-27 21:05 出处:网络
I\'m getting exceptions when copying content type from one web to another: foreach (SPContentType destinationWebCt in destinationWeb.ContentTypes)

I'm getting exceptions when copying content type from one web to another:

foreach (SPContentType destinationWebCt in destinationWeb.ContentTypes) 
{ 
    destinationWeb.ContentTypes.Add(existingWebCt); 
    destinationWeb.Update(); 
} 

existingWebCt is the content type from another web e.g. /Site/Web. destinationWeb is the web that I wish to copy the content type to e.g. /Site/DestinationWeb.

I got the SPException that says something like content type can not be added outside of its scope.

Then I decided to replace all "ContentTypes" to "AvailableContentTypes", but then I got the SPException saying this coll开发者_高级运维cetion could not be modified.

So how can I copy a content type to another web?

Thanks.


If you want content type to be available in another web, then you must have this content type in site content types collection or any parent web that these 2 web's have in common.

You could, ofcourse, create new SPContentType instance, copy all properties and then add it to another web, but it's a whole different content type then and it's seperated - if you make changes to one CT, it won't reflect in the other. So it is no way recommended and usefull.

And AvailableContentTypes property returns all content types of current web and parent webs, but are read-only content types so allows no modifications.

0

精彩评论

暂无评论...
验证码 换一张
取 消