Copied to clipboard

Flag this post as spam?

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


  • Andreas Bösig Vestergaard 4 posts 84 karma points
    Oct 02, 2017 @ 06:02
    Andreas Bösig Vestergaard
    0

    Displaying image with Umbraco API.

    I am trying to get the url for an image from my "updates" node in Umbraco. All other data is displaying perfectly, but the image url just says NULL. I can get the ID of the image, by removing the URL at the end.

    Do you have any idea about what im doing wrong?

    using System.Collections.Generic;
    using System.Threading.Tasks;
    using System.Web.Http;
    using crossmind.dk.Models;
    using Umbraco.Core.Models;
    using Umbraco.Web;
    using Umbraco.Web.WebApi;
    
    namespace crossmind.dk.Controllers
    {
        public class UpdateApiController : UmbracoApiController
        {
            [HttpGet]
            public async Task<IHttpActionResult> GetAllUpdates()
            {
                var updateList = new List<Update>();
                var updates = UmbracoContext.ContentCache.GetById(1061).Children();
    
                foreach (var updateItem in updates)
                {
                    updateList.Add(new Update
                    {
                        Title = updateItem.Name,
                        Date = updateItem.CreateDate.ToString("D"),
                        Url =  updateItem.Url,
                        ImageUrl = updateItem.GetPropertyValue<IPublishedContent>("billede")?.Url()
                    });
                }
                return Ok(updateList);
            }
        }
    }
    

    Thanks in advance, Andreas.

  • Alex Skrypnyk 6131 posts 23950 karma points MVP 7x admin c-trib
    Oct 02, 2017 @ 12:41
    Alex Skrypnyk
    0

    Hi Andreas

    Try this code: updateItem.GetPropertyValue

    Are you using latest Umbraco version?

    Are "Property value converters" enabled?

    Thanks,

    Alex

  • Craig Mayers 164 posts 508 karma points
    Oct 02, 2017 @ 15:38
    Craig Mayers
    101

    Hi Andreas,

    You can do the following also:

     var helper = new UmbracoHelper(UmbracoContext.Current);
     var imageUrl = helper.TypedMedia(updateItem.GetPropertyValue("aliasForYourImageeProperty")).Url;
    

    You may also, want to add in some null checking to your logic to ensure it doesn't fall over.

    Thanks

    Craig

  • Andreas Bösig Vestergaard 4 posts 84 karma points
    Oct 03, 2017 @ 07:13
    Andreas Bösig Vestergaard
    0

    That did the trick! Thank you so much :)

  • Alex Skrypnyk 6131 posts 23950 karma points MVP 7x admin c-trib
    Oct 02, 2017 @ 15:47
    Alex Skrypnyk
    0

    Hi Craig

    Will your code work if "Property value converters" will be enabled?

    Thanks,

    Alex

  • Craig Mayers 164 posts 508 karma points
    Oct 02, 2017 @ 16:29
    Craig Mayers
    0

    Hi Alex,

    If PropertyConverters are enabled the replace the code above with this:

    var imageUrl = updateItem.GetPropertyValue("aliasForYourImageeProperty").Url;
    

    As the type returned would be of IPublishedContent.

    Again, I would advise null checking the property before trying to assign it.

    Thanks

    Craig

Please Sign in or register to post replies

Write your reply to:

Draft