13 April, 2012

Umbraco Razor, DynamicMedia umbracoFile property empty

Some times a request for a DynamicMedia item can return an item where the property "umbracoFile" is empty/null.
dynamic mediaNode = new DynamicMedia(mediaNodeId);
var test2 = mediaNode.umbracoFile; // returning empty string

When searching for the problem on google it seems to be a problem only in version 4.7.1+ in umbraco.

As stated in theese two posts:
  1. DynamicNode MediaById umbracoFile returning empty string
  2. Razor Media Error on Upgrade to 4.7.1

It seems to be something with the way the media items are loaded in the dynamic node's because loading the same media node from xslt using the "umbraco.library.GetMedia" method does return the node with correct properties.

After some investigation into the issue I found out that it's actually 2 issues/bugs.

1. issue
This happens when using "<UseLegacyXmlSchema>true</UseLegacyXmlSchema>" in the umbraco 4.7.1+ (I'm using it due to our large installation, wich still uses a lot of old xslt files).
And the "umbraco.MacroEngines.ExamineBackedMedia.cs" file, it only expects the new content schema, and simply does not load any '<data alias="xx">' properties, but the code actually does load the node but not the child element values, when using the legacy schema in umbraco, it simple only accepts the schema style. 

2. issue 
The first issue only happens if the node is simply not found/missing in the Examine Indexes, this could properly happen in many ways, but in my case it was happening when cloneing the live DB to localhost for development, but it also sometines happens on live servers. (Still to investigate)

The solution(s)

To solve the first issue I modified the  "umbraco.MacroEngines.ExamineBackedMedia.cs" file so that if the item is not found in the examine index it will correctly load the node attributes and ' <data alias="xx"> ' nodes, see the workitem here (codeplex items have been removed) Instead the patch for Umbraco is now available in my dropbox, download it here.
But this still triggers a database request, if the node is not the runtime cache (30 minutes timeout), so I still wanted to solve the second issue also, so that the Examine Indexs would be correct, to optimize our umbraco installation.

To solve the second issue I installed the Examine Dashboard package, and rebuilt the indexes manually, I had hoped that a xml-republish (http;//YOURDOMAIN/Umbraco/dialogs/republish.aspx?xml=true) would do the trick, but it apprently does not republish the media nodes and/or the Examine Indexes. I will properly investigate this later