Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have a user control I'm using to edit new list items. In my Page_Load event, I am trying to retrieve the Content Type of the new item to use in further logic. But, I'm getting a null reference when I try to reference the content type. Am I going about this wrong? Here's my code:

protected void Page_Load(object sender, EventArgs e)
{
    SPContext context = SPContext.Current;

    SPListItem currentItem = context.ListItem;
    SPContentType currentCType = currentItem.ContentType;
    // Do some other logic
}
share|improve this question

1 Answer 1

up vote 0 down vote accepted

Try getting the ContentTypeId first (using currentItem["ContentTypeId"], then retrieve the ContentType from the List.ContentTypes collection.

share|improve this answer
    
Thanks for that idea. Unfortunately, for some reason, the ContentTypeId that is shown on the current item is not a real ContentTypeId. It starts with "0x00", which is not a valid ContentTypeId. In my situation, the ContentTypeId should start with "0x0102", since it is an "Event" (according to [link]msdn.microsoft.com/en-us/library/ms452896.aspx. –  Clay Acord Jul 20 '11 at 20:34
    
Try your code in CreateChildControls, PageLoad might be too early to get the context properly, maybe? –  James Love Jul 20 '11 at 21:44
    
I have tried CreateChildControls, OnPreRender, and RenderControls, and I can't see the "Event" content type. I can't see how Sharepoint can render the "NewItem" dialog without knowing the Content Type! –  Clay Acord Jul 21 '11 at 21:40
    
Wait, is this in the New Form or the Edit Form? If it's the New Form, the item hasn't been created and thus doesn't have a content type... –  James Love Jul 21 '11 at 21:42
    
It is in the New Form. That's the problem. Thanks, James! –  Clay Acord Jul 26 '11 at 14:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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