Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • djscorch 67 posts 106 karma points
    Feb 14, 2012 @ 12:47
    djscorch
    0

    ContentHelper.GetCurrentContent() error

    I'm trying to get the current template name, yet ContentHelper.GetCurrentContent() causes an "object not set" error...

    var templateName = ContentHelper.GetByNodeId(ContentHelper.GetCurrentContent().Template).Name;

    What am I doing wrong?

  • Vladan Ostojic 94 posts 210 karma points
    Feb 14, 2012 @ 12:58
    Vladan Ostojic
    0

    Where exactly are you trying to do this? Can you provide a full stack trace?

  • Sasa Popovic 77 posts 367 karma points
    Feb 14, 2012 @ 13:19
    Sasa Popovic
    0

    I believe the problem is that you are trying to get a template by calling "ContentHelper.GetByNodeId()" method. You are passing a template ID to method GetByNodeId which expects to get a node ID. The method returns null because it can't find a node with the ID of your template and then when you call ".Name" property on the result, it fails with an "object not set.." error.

     

  • djscorch 67 posts 106 karma points
    Feb 14, 2012 @ 14:10
    djscorch
    0

    Ok this produces the same error...

    var currentDocType = ContentHelper.GetCurrentContent();

  • djscorch 67 posts 106 karma points
    Feb 14, 2012 @ 14:12
    djscorch
    0

    Hold on...

    I'm working on a User Control that appears in the back end - so it won't have any content!

    Sorry

  • Vladan Ostojic 94 posts 210 karma points
    Feb 14, 2012 @ 14:15
    Vladan Ostojic
    0

    Ok, it looks like this problem is not related with the uSiteBuilder. What happens when you just call Node.GetCurrent() (getting the current Umbraco node without uSiteBuilder)?

    You can try it with the following code:

    umbraco.presentation.nodeFactory.Node node = umbraco.presentation.nodeFactory.Node.GetCurrent() 

     

    Which version of Umbraco are you using?

  • Vladan Ostojic 94 posts 210 karma points
    Feb 14, 2012 @ 14:18
    Vladan Ostojic
    0

    :) No problem

  • djscorch 67 posts 106 karma points
    Feb 14, 2012 @ 14:21
    djscorch
    0

    Same error: 

    Object reference not set to an instance of an object.

    I think it's because i'm trying to do this from the back end. (It's a custom control being displayed on a content tab using the usercontrol wrapper).

  • Vladan Ostojic 94 posts 210 karma points
    Feb 14, 2012 @ 14:27
    Vladan Ostojic
    0

    Yes, it's because of that. There is no current node in the backend's control. You can use uSiteBuilder as following:

    MyDocType doc = ContentHelper.GetByNodeId<MyDocType(123),  where 123 is id of a node of MyDocType document type OR

    DocumentTypeBase doc = ContentHelper.GetByNodeId(123), where 123 is node id. Then you test and cast doc object (e.g: if (doc is MyDocType) { mycodeType = (MyDocType)doc}  )

Please Sign in or register to post replies

Write your reply to:

Draft