Copied to clipboard

Flag this post as spam?

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


  • Christophe Vaught 21 posts 113 karma points
    Apr 10, 2018 @ 22:27
    Christophe Vaught
    1

    C# Cannot figure out how to convert umb://media/ to URL Umbraco 7.10

    Can someone help me out.

    public static ImageModel GetPageImage(Node node){ ImageTest = node.GetProperty

    Outputs umb://media/881492659b004247acbd24f97a8339a7

    I've tried numerous / numerous solutions with no avail.

    Can someone point me to straightforward c# solution that solves this issue . Or help explain to me how to solve.

    Model.Content.GetPropertyValue

    I'm not sure how to utilize the IPublishedContent

  • Daniel Chenery 119 posts 465 karma points
    Apr 11, 2018 @ 07:13
    Daniel Chenery
    1

    The short answer. Use Umbraco.TypedMedia()

    The long answer. Property Converters have been enabled by default, so you should be able to do var img = Model.Content.GetPropertyValue<IPublishedContent>("myImageAlias");

    Then you can render your image like so <img src="@img.Url" alt="@img.Name" />

    If you've needed to disable them for legacy reasons, the following code should work var img = Umbraco.TypedMedia(Model.Content.GetPropertyValue<Udi>('myImageAlias"));

  • Christophe Vaught 21 posts 113 karma points
    Apr 11, 2018 @ 14:10
    Christophe Vaught
    0

    I don't understand the solution above.

    Currently I am using umbraco.NodeFactory and using umbraco.cms.businesslogic.media.

    public static CalloutModel CalloutModelFromNode(Node node) { ImageTest = node.GetProperty

    Is it possible to return a URL using the above in Umbraco 7.10. I'd think someone would have wrote an tutorial on how to do this. I will do it if I ever get a detailed answer.

  • Daniel Chenery 119 posts 465 karma points
    Apr 11, 2018 @ 14:16
    Daniel Chenery
    0

    Hi Christophe,

    The response I've given you should work in Umbraco 7.10.

    I've not worked with the 2 namespaces myself, but I believe they're for legacy/older versions of Umbraco and (I may be wrong here), but I don't believe you should be using them.

    Perhaps you could share some more of your code, specifically the view, this will help us see what you're actually doing, and why it's not rendering.

    I've looked at the source code, and it looks like you might be able to do.

    var img = new umbraco.cms.businesslogic.media.Media(Model.Content.GetPropertyValue<Udi>('myImageAlias")), but I can't be 100% sure.

    The uses the MediaService though, which isn't performance friendly.

  • Christophe Vaught 21 posts 113 karma points
    Apr 11, 2018 @ 15:24
    Christophe Vaught
    0

    Do you have a sample of an entire C# class that can do this?

    What I am doing probably is not compatible. I run into issues when using using umbraco.cms.businesslogic.media; using Umbraco.Core.Models;

    I don't think they are compatible in same class.

    So what I am trying to do. Is get an Image's URL from the Media picker in a C# class.

    In the past I used umbraco.NodeFactory; Which still works for 99% of what I did in the past.

    Now I am running into nothing but issues trying to figure out how to get the IMAGE url from the Document Type Property which is a Media Picker.

    Not sure how you even set this up in New Umbraco as we had to shotgun this project within a small time span. Getting from Umbraco 6 to 7 took a bunch of time as it was.

  • Christophe Vaught 21 posts 113 karma points
    Apr 11, 2018 @ 15:25
    Christophe Vaught
    0

    Complete sample. Would be super helpful as many of us learn by examples. These snipets have been hard to understand

  • Daniel Chenery 119 posts 465 karma points
    Apr 11, 2018 @ 21:11
    Daniel Chenery
    0

    Where exactly are you trying to put this code? What I've provided should be able to dumped into a cshtml and will render image.

    If I can you're existing code, I might be able to assist further.

    I've only worked with Umbraco 7 though

  • Christophe Vaught 21 posts 113 karma points
    Apr 11, 2018 @ 22:22
    Christophe Vaught
    0

    The main problem I am running into is that I am using NodeServices & Uquery.

    I think I am asking the wrong question.

    How do I convert a string "umb://media/881492659b004247acbd24f97a8339a7 " into a URL.

    Is this even Possible.

    I'm refactoring some code hoping to solve this enigmatic problem

  • Daniel Chenery 119 posts 465 karma points
    Apr 12, 2018 @ 08:11
    Daniel Chenery
    0

    Hi Christophe,

    As per my first comment, Umbraco.TypedMedia(UDI_HERE) should return you an IPublishedContent, from there you can access the Url property.

    Let's step back a bit, where have you obtained this Udi/String from? How are you currently getting it?

    Your first post says you're using Model.Content.GetPropertyValue().

    If that is so, you should also be able to do Model.Content.GetPropertyValue<IPublishedContent>("myImageAlias").Url to directly get the URL (obviously you'd want to add some null checks in there though)

  • Christophe Vaught 21 posts 113 karma points
    Apr 12, 2018 @ 16:39
    Christophe Vaught
    0

    I'm not sure how to setup Model.Content in my current solution.

    Content is not an option for this

      var global = uQuery.GetNodesByType(UmbracoProperties.DocumentTypes.Page.Home)
                .FirstOrDefault();
    
  • Daniel Chenery 119 posts 465 karma points
    Apr 12, 2018 @ 19:51
    Daniel Chenery
    0

    Can you do global.GetProperty<Udi>("myImageAlias") instead? How have you been getting the Udi previously?

    Umbraco.TypedMedia("umb://media/881492659b004247acbd24f97a8339a7") should work I believe. Have you tried that at all?

    If not, you'll need Udi.Parse(...)

  • Christophe Vaught 21 posts 113 karma points
    Apr 12, 2018 @ 22:05
    Christophe Vaught
    0

    If I do

     var udiResult = Udi.Parse(node.GetProperty<string>("moduleImage"));
    

    that comes back with {umb://media/881492659b004247acbd24f97a8339a7}

            var test = node.GetProperty<Udi>("moduleImage");
    

    comes back with {umb://media/881492659b004247acbd24f97a8339a7}

    I'm not sure if this will help. In A razor view your code works fine.

    Exposing this in an API does not work as anticipated

  • Christophe Vaught 21 posts 113 karma points
    Apr 12, 2018 @ 22:30
    Christophe Vaught
    0

    This is the new way I've gone to try to get values. Still running into same issue.

     var ModelC = ApplicationContext.Current.Services.ContentTypeService.GetContentType("APiTestPage");
            var ModelCId = 13320; // Hardcoded for now
            var PageModel = ApplicationContext.Current.Services.ContentService.GetById(ModelCId);
            var imageProperty = PageModel.GetValue("singleImageTest");
            var udiResult = Udi.Parse(imageProperty.ToString());
    

    And the same damn result. BANG HEAD AGAINST WALL. Thee is something about the UDI that eludes me

  • Daniel Chenery 119 posts 465 karma points
    Apr 13, 2018 @ 07:58
    Daniel Chenery
    0

    In your example code, I can't see you using Umbraco.TypedMedia() anywhere.

    A UDI is simply a 'key' to the image. Every image has a unique one. They're a partial replacement for numeric IDs.

    As mentioned before, I'm not familar with the Umbraco 6 approaches such as uQuery, but I do believe that all you need to add to your code is this:

    var img = Umbraco.TypedMedia(udiResult);
    var imgUrl = String.Empty;
    if(img != null) {
        imgUrl = img.Url;
    }
    
    return imgUrl;
    

    By the way, the use of ApplicationContext is very bad for performance. It will it the database each time. I'd recommend swapping this out for var pageModel = Umbraco.TypedContentAtXPath("//APiTestPage") or a similar method.

  • Michael 125 posts 409 karma points
    Apr 13, 2018 @ 08:51
    Michael
    0

    Hey Christophe,

    You could try this:

    var id = Umbraco.GetIdForUdi(udi);
    

    and then proceed with normal media id.

    Thanks, Mike

  • Nik 1593 posts 7151 karma points MVP 6x c-trib
    Apr 13, 2018 @ 09:16
    Nik
    0

    Hi Christophe,

    Are you trying to use this code in Razor, or in a controller? If you are using this for front end behaviour, it is generally bad practice to be using the content services as these hit the database. Instead there are other ways to get the information.

    If you are in Razor, you can do the following to get your Media item URL:

    (Assumption made: a. your razor view inherits from UmbracoTemplatePage, b. the property is on the current document type, c. property has an alias of 'myMediaItem', d. you aren't using ModelsBuilder)

    //This line will get you the contents of the property
    var myMediaItem = Model.Content.GetPropertyValue<IPublishedContent>("myMediaItem");
    // Lets check that the above line didn't return null and if it didn't get the url from the published content, else provide an empty string.
    var mediaItemUrl = myMediaItem != null ? myMediaItem.Url : string.Empty;
    

    If you are using a SurfaceController then you can do the following (it's very similar and the same assumptions are being made)

    //This line will get you the contents of the property
    var myMediaItem = Umbraco.AssignedContentItem.GetPropertyValue<IPublishedContent>("myMediaItem");
    // Lets check that the above line didn't return null and if it didn't get the url from the published content, else provide an empty string.
    var mediaItemUrl = myMediaItem != null ? myMediaItem.Url : string.Empty;
    

    Now, with this approach we just need to be a little bit more careful as it is (although unlikely in my experience) possible that Umbraco.AssignedContentItem can be null so you might want to double check that.

    If you are using an API controller of some sort then things are a little different as you don't usually have a current page. But if you use the UmbracoHelper to find your page instead using node or content services you will be accessing the cache for the site. This means you will get IPublishedContent back and you can use the same premise discussed above to get your media details.

    Does that help explain things for you?

    With regards to UDI's, there are two types, as I understand it, there are GuidUDI's and Integer UDIs (I can't remember the class names for them both).

    The UDI isn't you url, it is simply an identifier for the content you are after. Once you have your UDI, you need to then look up the actual item using it. I think (I'm not 100%) that this can be done using the UmbracoHelper in a similar way to having the ID. This will then return an IPublishedContent (even for media), against which you can access the property URL which will give you the url of the page, or underlying media item.

    If things are still unclear, please keep asking. The more details you can provide about setup, configuration, use case, version of Umbraco etc the better. And if you can provide a full sample code snippet of your objective that is helpful as well (I know that isn't always possible).

    Thanks,

    Nik

  • Christophe Vaught 21 posts 113 karma points
    Apr 13, 2018 @ 15:24
    Christophe Vaught
    0

    Thanks to everyone for helping out. Michael's suggestion sorted out my issue! I really appreciated everyone's help. This is what worked for me!

    using Umbraco.Core;
    using Umbraco.Web;
    
    //Going to Need this
    UmbracoHelper uHelper = new UmbracoHelper(UmbracoContext.Current);
        var ModelC = ApplicationContext.Current.Services.ContentTypeService.GetContentType("APiTestPage");
                var TestI = ModelC.Name;
                var ModelCId = 13320;
                var PageModel = ApplicationContext.Current.Services.ContentService.GetById(ModelCId);
                var imageProperty = PageModel.GetValue("singleImageTest");
                var properties = PageModel.GetType().GetProperties();
                var udiResult = Udi.Parse((imageProperty.ToString()));
    
                var imgUrl = uHelper.TypedMedia(udiResult);
                string urlStr = imgUrl.Url;
    
                return new ImageModel
                {
                    Url = urlStr
                };
    
  • Daniel Chenery 119 posts 465 karma points
    Apr 14, 2018 @ 09:06
    Daniel Chenery
    0

    Glad you got it working in the end. Don't forget to mark this topic as resolved :)

  • David Armitage 505 posts 2073 karma points
    Jun 26, 2018 @ 08:07
    David Armitage
    1

    Hi Guys,

    Here is a method I use for this.

    public static IPublishedContent GetPublishedContentByUdi(string udi)
    {
        var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
        return umbracoHelper.TypedContent(udi);
    }
    

    Hope it helps,

    Kind Regards

    David

Please Sign in or register to post replies

Write your reply to:

Draft