x First time here? Check out the FAQ
  • Avatar13posts53karma

    Razor exception getting NodeById

    Andrew Duff started this topic March 11, 2012 @ 11:07, Go directly to the topic solution

    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 posted this reply March 11, 2012 @ 11:19

    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 login or Sign up To post replies