简体   繁体   中英

How to get Content Type by Name or ID?

I'm trying to get a Content Type by the Name or the ID.

I'm using this code but it does not work.

SPContentTypeId eventTypeId = new SPContentTypeId("0x0102"); 
SPContentType eventType = web.AvailableContentTypes[eventTypeId];

Do you have any ideas?

Thanks.

Based On the the information you have provided in your question, I think what you are looking for is something like this:

SPContentType spct = web.AvailableContentTypes ["ContentTypeName"];

Good luck! :)

Just a note on the above. If you plan on making ANY changes to the content type then you don't want to use the above function. It will return read only versions. You want to be using:

web.ContentTypes["ContentTypeName"].

Instead of:

web.AvailableContentTypes["ContentTypeName"].

Just to save people a whole pile of time as the above answer is not really correct. It should include BOTH options as it really depends on the context and how they are using it. Although they don't give those sorts of details this is a top answer in google.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM