Copied to clipboard

Flag this post as spam?

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


  • daniel 13 posts 83 karma points
    Jun 20, 2017 @ 16:22
    daniel
    0

    Trying to get Image from Node

    So I have a list of nodes. While looping through the nodes, I'm trying to get the image src using

    src="@node.GetProperty("postImage").Value"

    however, this returns something like

    { "focalPoint": { "left": 0.5, "top": 0.5 }, "src": "/media/9494/testxx12_1200x628-1.png", "crops": [ { "alias": "blogPost", "width": 200, "height": 200 }, { "alias": "thumbnail", "width": 50, "height": 50 }, { "alias": "featuredImage", "width": 320, "height": 238 } ] }

    how do i just get the src? Sorry if this has already been asked, but I could not find anything on the forums for this exact scenario.

    thanks

  • Marc Goodson 2155 posts 14408 karma points MVP 9x c-trib
    Jun 20, 2017 @ 18:48
    Marc Goodson
    0

    Hi Daniel

    What version of Umbraco are you using?

    and what is your code and objects that you are looping through.

    Generally these days, everything is using IPublishedContent or Modelsbuilder

    and you'll see the documentation here:

    https://our.umbraco.org/documentation/reference/querying/umbracohelper/

    and here for getting crops:

    https://our.umbraco.org/documentation/getting-started/backoffice/property-editors/built-in-property-editors/image-cropper

    I ask the question though because you mention looping through Nodes and the syntax you've posted is similar to what would have been used on older Umbraco sites or if using uQuery, these approaches are now obsolete, but you might be maintaining an old site, so thought best to ask first rather than outlining the shiny new way that might not be available to you!

    regards

    Marc

  • daniel 13 posts 83 karma points
    Jun 20, 2017 @ 18:59
    daniel
    0

    Hello Marc,

    I'm using Umbraco version 7.5.6.

    my code looks like

    var list = uQuery.GetNodesByType("ArticulateRichText");
    
    List<umbraco.NodeFactory.Node> relatedList = new List<umbraco.NodeFactory.Node>();
    
    
    
    @foreach (var node in relatedList)
                                        {
                                            if (count <= 1)
                                            {
                                                var image = node.GetProperty("postImage").Value; 
                                            }}
    

    this is using razor in the cshtml.

    thanks,

  • Marc Goodson 2155 posts 14408 karma points MVP 9x c-trib
    Jun 20, 2017 @ 19:55
    Marc Goodson
    1

    Hi daniel

    Yes, using uQuery is quite an old approach, the code you have, doesn't work because Umbraco has changed the way that it stores it's umbracoFile property details to be a JSON blob of crop information rather than the url to the image - if you are using Umbraco 7.5.6 it may be more appropriate to use the Umbraco Helpers:

    https://our.umbraco.org/documentation/reference/querying/umbracohelper/

    So you could have something like:

    @{
    
        var blogPosts = Umbraco.TypedContentAtXPath("//ArticulateRichText");
    
    }
    
    @foreach (var relatedPost in blogPosts){
    //not sure what count is in your example... or whether postImage is a media picker or an image cropper, assuming mediaPicker: 
    
    var postImage = relatedPost.HasProperty("postImage") && relatedPost.HasValue("postImage") ?  Umbraco.TypedMedia(relatedPost.GetPropertyValue("postImage")) : default(IPublishedContent);
    
    if (postImage!=null) {
    <img src="@postImage.Url" alt="@postImage.Name" />
    }
    }
    

    if that at least helps understand why it's not working for you!

    (depending on the site of your site, to get all blog posts based on their alias, it might be a better approach to use Examine)

  • daniel 13 posts 83 karma points
    Jun 20, 2017 @ 20:39
    daniel
    0

    thank you! Following your link i was able to come up with the following solution

    var test = Umbraco.Content(node.Id);
    var image = test.GetPropertyValue("postImage").src;
    

    And sure enough that got me the image link

Please Sign in or register to post replies

Write your reply to:

Draft