How to get Content Type by Name or ID?

14,572

Solution 1

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! :)

Solution 2

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.

Share:
14,572
carruw
Author by

carruw

Updated on June 04, 2022

Comments

  • carruw
    carruw almost 2 years

    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.