Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Mike 80 posts 101 karma points
    Feb 24, 2014 @ 15:49
    Mike
    0

    Using umbraco.library.getmedia in C# instead of GetMedia

    Hi,

    I have C# code that uses code below to get an image from a media folder.  

    Media folder = new Media(mediaFolderID);
    Media firstImage = folder.Children[0];
    url = firstmage.getProperty("umbracoFile").Value.ToString();

    This code however is too slow. So I need a better solution. I use umbraco.library.getMedia in XLST and that works great, also I read this post http://shazwazza.com/post/Ultra-fast-media-performance-in-Umbraco which says that getmedia works faster. 

    the code from the website does not take folders into account so this will not work for me. My knowledge of xpAthIterator in C# is not very good so i need some help here.

    I can get the correct folder and XML like this

    XPathNodeIterator media = umbraco.library.GetMedia(mediaFolderID,true);

    Which outputs as ( media.Current.OuterXML.ToString()):

    <node path="-1,1131,5714" nodetypealias="Folder" writername="Administrator" urlname="urlnameoffolder" 
    nodename="nodeName Of Folder" updatedate="2013-08-27T14:07:54" 
    createdate="2013-08-27T14:07:54" sortorder="0" template="0" nodetype="1031" 
    writerid="0" level="2" parentid="1131" version="7e45a921-4fd3-4399-aa53-866750a225c2" id="5714">

    <data alias="contents">

     <node path="-1,1131,5714,5715" nodetypealias="Image" writername="administrator" 
     urlname="urlname.jpg" nodename="1961 abc.jpg" 
     updatedate="2013-08-27T14:07:54" createdate="2013-08-27T14:07:54" sortorder="1" template="0" nodetype="1032" 
     writerid="1" level="3" parentid="5714" version="e1034024-52bf-423f-8119-ca94675be031" id="5715">
    <data alias="umbracoFile">/media/198119/1961 abc.jpg</data>
    <data alias="umbracoWidth">1000</data>
    <data alias="umbracoHeight">667</data>
    <data alias="umbracoBytes">215084</data>
    <data alias="umbracoExtension">jpg</data>
     </node>
     
     <node path="-1,1131,5714,5715" nodetypealias="Image" writername="administrator" 
     urlname="urlname.jpg" nodename="1961 abc.jpg" 
     updatedate="2013-08-27T14:07:54" createdate="2013-08-27T14:07:54" sortorder="1" template="0" nodetype="1032" 
     writerid="1" level="3" parentid="5714" version="e1034024-52bf-423f-8119-ca94675be031" id="5715">
    <data alias="umbracoFile">/media/198119/1961 abc.jpg</data>
    <data alias="umbracoWidth">1000</data>
    <data alias="umbracoHeight">667</data>
    <data alias="umbracoBytes">215084</data>
    <data alias="umbracoExtension">jpg</data>
     </node>
     <node path="-1,1131,5714,5715" nodetypealias="Image" writername="administrator" 
     urlname="urlname.jpg" nodename="1961 abc.jpg" 
     updatedate="2013-08-27T14:07:54" createdate="2013-08-27T14:07:54" sortorder="1" template="0" nodetype="1032" 
     writerid="1" level="3" parentid="5714" version="e1034024-52bf-423f-8119-ca94675be031" id="5715">
    <data alias="umbracoFile">/media/198119/1961 abc.jpg</data>
    <data alias="umbracoWidth">1000</data>
    <data alias="umbracoHeight">667</data>
    <data alias="umbracoBytes">215084</data>
    <data alias="umbracoExtension">jpg</data>
     </node>  
     
    </data>
    </node>

    From this XML I need the umbracoFile value of the first image. I cannot seem to get this working. 

    Hope somebody can help me with this thanks!

     

     

     

     

     

     

     

     

     

  • Mike 80 posts 101 karma points
    Feb 24, 2014 @ 17:38
    Mike
    0

    Hi, I found something. I'm still not sure if this is correct because it still takes a long time in the loop (varying from 10-40ms depending on number of images in folder) when I include this code.

    XPathNodeIterator media = umbraco.library.GetMedia(mediaFolderID,true); 
    string filename = string.empty;
    try {
    filename = media.Current.SelectSingleNode("/node/node/data[@alias='umbracoFile']").InnerXml;
    }catch (Exception) {
    filename = "images/unknown.png");
    }
Please Sign in or register to post replies

Write your reply to:

Draft