Search In
Learn from 350 other Umbracians at the annual Umbraco Conference - CodeGarden '13. More than twenty high quality sessions, open spaces, hackathons and social events you'll remember. Not to be missed! Less than 25 tickets left - get yours now!
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
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.