The sample Tom refers to uses the Media api which is pretty slow. I recommend you use library.GetMedia because it uses the umbraco cache system. You can read all the data from the XPathNodeIterator which GetMedia returns. Here is a sample
//Get the media item from the umbraco cache.
XPathNodeIterator mediaIterator = library.GetMedia(nodeId, false);
//Convert the XPathNodeIterator to an XDocument.
XDocument xmlDocument = XDocument.Parse(mediaIterator.Current.OuterXml);
//Get the root element.
XElement mediaElement = xmlDocument.Root;
string mediaUrl = GetMediaLink(mediaElement)
/// <summary>
/// Return the media link of an element.
/// </summary>
/// <param name="mediaElement"></param>
/// <returns></returns>
public static string GetMediaLink(XElement mediaElement)
{
XElement filePathElement = DigibizMediaHelper.GetFilePathElement(mediaElement);
if (filePathElement == null)
{
return "#";
}
return filePathElement.Value;
}
/// <summary>
/// Get the file path element. If the old xml schema is used we need to fetch this a different way.
/// </summary>
/// <param name="mediaElement"></param>
/// <returns></returns>
public static XElement GetFilePathElement(XElement mediaElement)
{
XElement filePathElement =
!UmbracoSettings.UseLegacyXmlSchema
?
mediaElement.Descendants("umbracoFile").FirstOrDefault()
:
mediaElement.Descendants("data").Where(x => x.Attribute("alias").Value == "umbracoFile").FirstOrDefault()
;
if (filePathElement == null)
{
//If the filepath is not found try another option.
filePathElement =
!UmbracoSettings.UseLegacyXmlSchema
?
mediaElement.Descendants("fileName").FirstOrDefault()
:
mediaElement.Descendants("data").Where(x => x.Attribute("alias").Value == "fileName").FirstOrDefault()
;
}
if (filePathElement == null)
{
//If the filepath is still not found try a third option.
filePathElement =
!UmbracoSettings.UseLegacyXmlSchema
?
mediaElement.Descendants("file").FirstOrDefault()
:
mediaElement.Descendants("data").Where(x => x.Attribute("alias").Value == "file").FirstOrDefault()
;
}
return filePathElement;
}
Finding media url
How do I get url to my media programatically?
With xslt Ive succesfully managed with the following:
<xsl:variable name="mediaId" select="number(./image)" />
<xsl:if test="$mediaId > 0">
<xsl:variable name="mediaNode" select="umbraco.library:GetMedia($mediaId, 0)" />
<xsl:if test="$mediaNode/umbracoFile">
<img src="{$mediaNode/umbracoFile}" alt="[image]" height="170"/>
</xsl:if>
</xsl:if>
I want to use C# to achieve this in visual studio!
this does not work.
Hi Frederik,
Check out this post, think it will do the trick: http://our.umbraco.org/forum/developers/api-questions/7993-Get-URL-of-media-file#comment29410
-Tom
The sample Tom refers to uses the Media api which is pretty slow. I recommend you use library.GetMedia because it uses the umbraco cache system. You can read all the data from the XPathNodeIterator which GetMedia returns. Here is a sample
This is how I do it in the Digibiz Advanced Media Picker.
Jeroen
is working on a reply...