I am trying to get the ContentType.Name of the Event (SPListItem) however it is NULL when I try to access it. I'm getting a value for the ContentTypeID, so I would assume that I would be able to access the ContentType name.

ContentType Null

Here is a general outline of my code, using a CAML query to access the list, then looping through

        using (SPWeb EventsSite = SPContext.Current.Site.OpenWeb(eventSiteURL.TrimEnd('/')))
            SPList EventsList = EventsSite.GetList(EventsSite.ServerRelativeUrl.TrimEnd('/') + "/Lists/Events");
            SPQuery EventQuery = new SPQuery();
            EventQuery.ViewFields = @"<FieldRef Name='Event_x0020_Description' /><FieldRef Name='Event_x0020_Name' /><FieldRef Name='EventStartDate' /><FieldRef Name='EventEndDate' />
                                      <FieldRef Name='Event_x0020_Tuition' /><FieldRef Name='EventMeals' /><FieldRef Name='EventCancellation' /><FieldRef Name='EventSeats' />;
            EventQuery.Query = string.Format(@"<Where><Eq><FieldRef Name='Associated_x0020_Site' /><Value Type='Text'>{0}</Value></Eq></Where>", SPContext.Current.Web.ID);
            Event = EventsList.GetItems(EventQuery)[0];

// Event.ContentType.Name is NULL ?
var MyEventName = Event.ContentType.Name;
  • 1
    What is the event type? ItemAdded or ItemAdding? – Eric Alexander May 28 '15 at 14:35
  • 2
    Can you try checking SPListItem["ContentType"] as a field value? – Arsalan Adam Khatri May 28 '15 at 14:35
  • Thanks Arsalan, SPListItem["ContentType"] worked I had the incorrect syntax when I had tried that previously. – TreK May 28 '15 at 18:44

If you add "ContentType" to the ViewFields, you can access the name of the content type with the indexer of the SPListItem:

SPListItem item = ...;
string contentTypeName = item["ContentType"] as string;
| improve this answer | |

When you specify ViewFields in a query, you will get values only for the selected fields in your result. Try to remove ViewFields from your query.

| improve this answer | |
  • I tried adding <FieldRef Name='ContentType' /> to my ViewFields but didn't get any results. Should that work to return the ContentType object? – TreK May 28 '15 at 14:42
  • No. I think it doesn't work, because it's not an item field value. If you don't want to get the all SPListItem object, you can try to get SPContentType from the list by the GUID, like this: 'var ct = EventsList.ContentTypes[Event.ContentTypeId]', and then you can get the name like ct.Name – ScienceSoft team May 28 '15 at 14:53
  • Thank you for the reply. I looked through all of the ID's in the EventsList.ContentTypes and the Event.ContentTypeId doesn't exsist in there? I currently have 6 content types that show up in my ContentTypes collection, but non of the ID's match the ContentTypeId, any idea why these would not match? – TreK May 28 '15 at 17:39

You can find SPContentType using the ID value as below

SPContentTypeId CTypeId = new SPContentTypeId(Event["ContentTypeId"]);
SPContentType CType = web.AvailableContentTypes[CTypeId];

Now use

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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