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 4.7.1.1 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

4 comments:

  1. This is still an issue in Umbraco 6.0, and very frustrating. Same issue: returns umbracoFile for older media items, fails for anything added recently. Frustrating to have to rebuild indexes, and have no idea when they're likely to need rebuilding in the future.

    ReplyDelete
  2. Hi Eric, I have not yet updated our Umbraco install past v4.7.XX, so I have no v6 experience with this issue, but properly the same bug still exists there. And I have not found any issues on the new bugtracker for Umbraco that involves this issue.
    We are going to start working on a new v6 Umbraco install, where we will have to re-patch some of our old fixes to get it running smoothly, I will keep this blog/post updated with the progress and submit patches to the core team, once we get started.

    ReplyDelete
  3. Any update on the progress of this fix in 6.0?

    ReplyDelete
  4. Hi Steph, sorry not to my knowledge, are you having the problem in v6 of Umbraco also (I thought it was fixed by HQ there)

    We have not yet migrated to v6 of Umbraco, so I have not tested it there yet. But we are going to start some preliminary work on v6 in about 3 months time, but properly you cant wait that long.

    ReplyDelete