Copied to clipboard

Flag this post as spam?

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


  • Bex 444 posts 555 karma points
    Aug 10, 2017 @ 11:03
    Bex
    0

    U7 Get Media URL in C# name spaces?

    Hi

    This should be simple but I am missing something obvious.

    I have a web service and I am getting my documents within it and I need to get the url of an image uploaded via a media picker.

    My code is as follows:

    foreach(var d in documents)
                    {
                        string name = d.GetProperty("documentTitle").Value.ToString();
    
                        var media=Umbraco.Medi(d.GetProperty("documentUpload").Value.ToString());
                    }
    

    I have read in multiple places I should be able to get the media picker by using

    Umbraco.Media(d.GetProperty("documentUpload").Value.ToString())
    

    But I seem to be missing a name space or something as it never finds "Media" under Umbraco in my code.

    I have the following usings:

       using Umbraco.Core;
        using Umbraco.Core.Models;
        using Umbraco.Web;
    

    What am I missing? Or am I doing this wrong?

  • Bex 444 posts 555 karma points
    Aug 15, 2017 @ 09:09
    Bex
    0

    Any one got any advice to offer?

  • Kevin Jump 2115 posts 13361 karma points MVP 5x c-trib
    Aug 15, 2017 @ 09:16
    Kevin Jump
    100

    Hi

    you need to either pass or generate a version of the UmbracoHelper class.

    in razor views the Umbraco object. is a version of the Umbraco Helper.

    it depends a bit on what your webservice is.

    if you are inheriting an umbraco controller via UmbracoController or UmbracoApiController (or the authorized versions if your webservice is only for back office use) then you will have an Umbraco object which is the Umbraco Helper.

    if not. then the you can create the helper in code

        var umbHelper = new UmbracoHelper(UmbracoContext.Current);
    

    and from here you should be able to do umbHelper.Media()

    i would also look at .TypedMedia() because that gets you a typed version of the media object, which is slightly better to work with (and i think dynamic types are on the way out)

  • Bex 444 posts 555 karma points
    Aug 15, 2017 @ 09:20
    Bex
    0

    Thank you @Kevin!

    That was exactly what I needed.

Please Sign in or register to post replies

Write your reply to:

Draft