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!
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;
}}
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:
@{
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)
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
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
Hello Marc,
I'm using Umbraco version 7.5.6.
my code looks like
this is using razor in the cshtml.
thanks,
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:
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)
thank you! Following your link i was able to come up with the following solution
And sure enough that got me the image link
is working on a reply...