Copied to clipboard

Flag this post as spam?

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


  • Rob Stevenson-Leggett 38 posts 110 karma points
    Dec 10, 2010 @ 17:06
    Rob Stevenson-Leggett
    0

    umbraco.library.GetMedia only finding node on first try after Republish Entire Site

    Hi,

    Strange problem with a 4.5.2 site.

    I'm using umbraco.library.GetMedia in some backend code. Here's the code:

    public string GetMediaFile(string nodeId)
    {
          string url = String.Empty;
          int mediaId = 0;
          if (Int32.TryParse(nodeId, out mediaId))
          {
              XPathNodeIterator xn = umbraco.library.GetMedia(mediaId, false);
    //This movenext fails on the second request
              if (xn.MoveNext())
             {
                  XPathNodeIterator xn2 = xn.Current.Select("data[@alias='umbracoFile']");
                 if (xn2.MoveNext())
                  {
                  url = xn2.Current.Value;
                  }
              }
         }
           return url;
    }

    The thing is it only returns the media item on the first request after a republish.

    Very strange. Would anyone have any idea why this might be happening?

  • Rob Stevenson-Leggett 38 posts 110 karma points
    Dec 10, 2010 @ 17:10
    Rob Stevenson-Leggett
    0

    Just discovers that setting the umbraco library cache duration to 0 fixes the problem - so something is wrong with the cache? Or should I be using another method to get media url in codebehind?

    <UmbracoLibraryCacheDuration>0</UmbracoLibraryCacheDuration>
  • Morten Bock 1867 posts 2140 karma points MVP 2x admin c-trib
    Dec 10, 2010 @ 18:34
    Morten Bock
    0

    I was looking at the source, and it seems that there might be a bug.

    What does it return if you do this:

    public string GetMediaFile(string nodeId)
    {
        string url = String.Empty;
        int mediaId = 0;
        if (Int32.TryParse(nodeId, out mediaId))
        {
            XPathNodeIterator xn = umbraco.library.GetMedia(mediaId, false);
            //This movenext fails on the second request
            if (xn.MoveNext())
            {
                return xn.Current.OuterXml;
            }
        }
        return url;
    }

    I'm guessing you might get something like "<error>No media is maching '{0}'</error>"

  • Carlos 338 posts 472 karma points
    Aug 06, 2012 @ 20:21
    Carlos
    0

    We have had the same issue happen to use for about 6-7 months now and could not figure out the issue, but it was happening with our XSLT files.  Thanks for the find, but that is kind of a horky fix.  Caching would be nice.  Is there a fix? We are using Umbraco 4.7.1

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies