Copied to clipboard

Flag this post as spam?

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


  • David F. Hill 122 posts 242 karma points
    Mar 21, 2010 @ 02:49
    David F. Hill
    1

    Get URL of media file

    Hello Umbraco Colleagues,

    How can I retreive the URL of files stored in the media section of Umbracon 4.0.2.1 in C#?

    The property in the doc type for these files is a Media Picker.

    I need to show a list of hyperlinks to PDFs that are stored as media files in Umbraco. When the user clicks on a link, the PDF will load.

    Should be simple, but I haven't figured it out yet. Any help would be greatly appreciated!

    Thanks,

    David Hill

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Mar 21, 2010 @ 06:47
    Jan Skovgaard
    0

    Hi David

    I think you can use the umbraco.library:GetMedia function in C# even though I just normally use it in XSLT instead.

    If you just need to make a list of PDF's I think I would go with XSLT.

    There is a good example of this in the WIKI section about the umbraco.library, which you can see here http://our.umbraco.org/wiki/reference/umbracolibrary/getmedia

    Assuming that you have a MediaPicker property set on your node referencing a folder in the media library the XSLT could look something like this

     

    <ul>

    <xsl:for-each select="umbraco.library:GetMedia($currentPage/data [@alias = 'pdfFolder'], 'false')/descendant::node">

    <li>
    <a href="{umbraco.library:GetMedia(@id, 'false')/data [@alias = 'umbracoFile']}"><xsl:value-of select="@nodeName" /></a>

    </li>

    </xsl:for-each>

    </ul>

     

    Hope this is usefull.

    /Jan

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Mar 21, 2010 @ 06:49
    Jan Skovgaard
    0

    Just discovered this WIKI-entry by Warren, which may be helpfull to you if you choose to go with the XSLT approach: http://our.umbraco.org/wiki/reference/code-snippets/listfilesfrommediafolderxslt

  • Nik Wahlberg 639 posts 1237 karma points MVP
    Mar 21, 2010 @ 14:25
    Nik Wahlberg
    11

    Hi David,

    Here's how you would go about getting your file URL:

    //mediaId is the ID returned from your document property...
    Media file = new Media(mediaId);
    string url = file.getProperty("umbracoFile").Value.ToString();

    HTH,
    Nik

  • David F. Hill 122 posts 242 karma points
    Mar 21, 2010 @ 19:42
    David F. Hill
    0

    Thanks everyone.

    I appreciate all the input. I need to go with the C# approach for this requirement because it's part of a user control (I could probably use some in-line XSLT for  that).

    Nik - your solution works like a charm - exactly what I was looking for. Thanks.

    David

  • Nik Wahlberg 639 posts 1237 karma points MVP
    Mar 22, 2010 @ 14:26
    Nik Wahlberg
    0

    Perfect, glad that worked out. Yeah, sometimes XSLT just won't do :) Don't get me wrong, I love it when I can use it...

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Mar 22, 2010 @ 14:46
    Jan Skovgaard
    0

    Nik - maybe you could write a simple sample in the WIKI on how to get meida files with C#...that would be cool :-)

    /Jan

  • Nik Wahlberg 639 posts 1237 karma points MVP
    Mar 22, 2010 @ 17:31
    Nik Wahlberg
    1

    Sure, I'd be happy to...i'll add it shortly. 

    -- Nik

  • Geoffrey Wadsworth 4 posts 24 karma points
    May 22, 2013 @ 01:17
    Geoffrey Wadsworth
    0

    I am in umbraco v6.+

    I am looking for a way to build a list of all media added to the media content.

    After poking around I came up with:

    public void GetMediaFileList()
        {
            int[] ids = Media.getAllUniqueNodeIdsFromObjectType(new Guid("B796F64C-1F99-4FFB-B886-4BF4BC011A9C"));       
            List<string> fileNames = new List<string>();

            string url = String.Empty;
            foreach (int mediaId in ids)
            {                       
                Media file = new Media(mediaId);

                if (file.ParentId > 0)
                {
                    //url = file.getProperty("fileName").Value.ToString();
                    url = file.getProperty("umbracoFile").Value.ToString();
                    int pos = url.LastIndexOf(@"/");
                   
                    fileNames.Add(url.Substring(pos + 1));
                }
            }
           
            sendJSON(new { files = fileNames });
        }

    The out put from this is (I only have three items in the media folder at this point):

    {"files":["careers.png","donations_t.jpg","1.jpg"]}

    So it is working.  However, there are a couple of things I do not like about this.  Feels like a hack.  More importantly the Media object is marked as depricated.

    If there is a better more up to date way to do this I would appreciate it.

    Thanks.

     

     

     

     

  • Tom Fulton 2030 posts 4998 karma points c-trib
    May 22, 2013 @ 04:38
    Tom Fulton
    0

    Hi Geoffrey,

    It sounds like you're using the old APIs - in v6, new (cleaner, better, shinier) APIs were introduced.

    There doesn't seem to be much documentation on the MediaService at the moment, but I imagine it's similar to ContentService and should be self explanatory if you look at the methods in autocomplete.  There is some documentation on the new Media object that you'll get back when using the MediaService, though.

    Hope this helps get you started!

    -Tom

  • Geoffrey Wadsworth 4 posts 24 karma points
    May 22, 2013 @ 18:03
    Geoffrey Wadsworth
    0

    Thanks Tom.  Huge improvement.

    Finding the right documentation has been a bit tricky. 

    This code does the same thing as the code above:

        public void GetMediaFileList()
        {
            List fileNames = new List();
            var mediaFiles = ApplicationContext.Current.Services.MediaService.GetDescendants(1198);

            string url = String.Empty;
            foreach (Media file in mediaFiles)
            {
                url = file.GetValue("umbracoFile").ToString();
                
                fileNames.Add(Path.GetFileName(url));
            }
            
            sendJSON(new { file = fileNames });
        }

    The only thing that I might want to tinker with is that you have to start from a known Media Folder Id. I assume it is possible to create multiple media folders and if that is the case this code would not find all the media.  It might be desireable to be able find media regardless of what folder it is in.

     

     

  • Tom Fulton 2030 posts 4998 karma points c-trib
    May 22, 2013 @ 18:51
    Tom Fulton
    0

    Very nice!  Glad that helped.

    Are you wanting to search all media or only under a certain folder?  You might have a play with GetRootMedia, which I think returns all items at the root level, then check their descendants.  Or if you're only looking for a certain media type, maybe GetMediaOfMediaType?

  • Geoffrey Wadsworth 4 posts 24 karma points
    May 22, 2013 @ 19:02
    Geoffrey Wadsworth
    0

    If you change this one line:

    var mediaFiles = ApplicationContext.Current.Services.MediaService.GetRootMedia();

    You get nothing back.  Not sure why.

    My current solution should work as we were planning on having them use one folder in particular for this functionality.  I was just trying to be proactive as I am sure someone will one day create a new folder and then wonder where their content is. 

    I also think it is probably more likely that we will want to add content that is not returned in the search mechanism I am putting together.  If that is the case the current solution will be best.

    Thanks again.

  • Tom Fulton 2030 posts 4998 karma points c-trib
    May 22, 2013 @ 19:07
    Tom Fulton
    0

    Hmm, strange.  Maybe a bug worth reporting on the issue tracker.

    Last suggestion, you could add a media picker somewhere on your site to select which folder to use for this script, then pull in that ID dynamically.  That way it doesn't require a code change when the folder changes later.

  • Geoffrey Wadsworth 4 posts 24 karma points
    May 22, 2013 @ 19:10
    Geoffrey Wadsworth
    0

    Interesting idea about the media picker.  As the site grows I will keep it in mind.

    New to umbraco so I am unfamiliar with the issue tracker.  I will take a look for it. 

  • Tom Fulton 2030 posts 4998 karma points c-trib
    May 22, 2013 @ 19:12
  • Steven 12 posts 83 karma points
    May 30, 2013 @ 09:54
    Steven
    0

    Hi Geoffrey,

    Perhaps what you're looking for then is the "GetMediaOfMediaType(int id)" method of the Umbraco v6 API MediaService. The `id` parameter is the ID of the MediaType you would like to retrieve/use.

    Check out the documentation on the MediaService, I've recently added all methods to the MediaService page;
    http://our.umbraco.org/documentation/reference/management-v6/services/MediaService

    --Steven

  • David Armitage 510 posts 2081 karma points
    May 18, 2018 @ 02:40
    David Armitage
    0

    Hi Guys,

    It looks like this has changed slightly in the new versions of the URL. It is no longer getProperty but is now GetValue("umbracoFile")....

    public static string GetMediaUrl(IMedia media)
    {
        return media.GetValue("umbracoFile") != null ? media.GetValue("umbracoFile").ToString() : string.Empty;
    }
    

    Hope this helps.

    Kind Regards

    David

Please Sign in or register to post replies

Write your reply to:

Draft