Copied to clipboard

Flag this post as spam?

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


  • Andrew Duff 13 posts 53 karma points
    Mar 11, 2012 @ 11:07
    Andrew Duff
    0

    Razor exception getting NodeById

    Hi, I'm attempting to write a Razor script by having a issue when a Node has been deleted by the reference still exists within a node property.

    Using Library.NodeById(<Id>) I get an exception

    System.NullReferenceException: Object reference not set to an instance of an object.
       at umbraco.MacroEngines.ExamineBackedMedia.GetUmbracoMedia(Int32 id)
       at umbraco.MacroEngines.DynamicBackingItem..ctor(Int32 Id)
       at umbraco.MacroEngines.DynamicNode..ctor(Int32 NodeId)
       at umbraco.MacroEngines.Library.RazorLibraryCore.NodeById(Int32 Id)
       at ASP._Page_macroScripts_ProductCategorySidebar_cshtml.Execute()

    Is there a method that exists to check if the node exists instead of enclosing within a try catch to avoid the exception stopping the render?


    For a temporay fix I have added the code below as a function. I start from -1 as the object I require exist within a Global section in the node tree where I hold Banners, Promotions etc for better site structuring.

        public dynamic GetNodeById (int Id)
        {
            DynamicNodeList colNodes = ((DynamicNode)Library.NodeById(-1)).Descendants(n => n.Id == Id);        
            if (colNodes != null &&             colNodes.Items.Count != 0)         {             return colNodes.Items[0] as dynamic;         }
            return null;     }

    Thanks Andy

  • Andrew Duff 13 posts 53 karma points
    Mar 11, 2012 @ 11:19
    Andrew Duff
    0

    I should I read the

    umbraco.MacroEngines.ExamineBackedMedia.GetUmbracoMedia(Int32 id)

    Didn't realize it uses examine and just updated it, didn't work so was debugging for got to put the InternalIndexSet back. Working now.

Please Sign in or register to post replies

Write your reply to:

Draft