0

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
}

1 Answer 1

0

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

5
  • 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, 2011 at 20:34
  • Try your code in CreateChildControls, PageLoad might be too early to get the context properly, maybe?
    – James Love
    Jul 20, 2011 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, 2011 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, 2011 at 21:42
  • It is in the New Form. That's the problem. Thanks, James!
    – Clay Acord
    Jul 26, 2011 at 14:31

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.