Skip to content

Instantly share code, notes, and snippets.

@mortenbock
Created May 21, 2014 10:46
Show Gist options
  • Save mortenbock/e21f4a00e068c02f0ab1 to your computer and use it in GitHub Desktop.
Save mortenbock/e21f4a00e068c02f0ab1 to your computer and use it in GitHub Desktop.
Media fallback
public static string GetOptionalMediaUrl(this INode node, string propertyAlias)
{
int? mediaId = node.GetOptionalNullableValue<int>(propertyAlias);
if (mediaId.HasValue)
{
XPathNodeIterator media = library.GetMedia(mediaId.Value, false);
//we look at both //Image and //node, because some media items may not have been saved since the schema changed.
var file = media.Current.SelectSingleNode("//Image/umbracoFile | //node/data[@alias='umbracoFile']");
if (file != null)
return file.InnerXml;
}
return string.Empty;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment