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);
}
}
}
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.
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?
Thanks in advance, Andreas.
Hi Andreas
Try this code: updateItem.GetPropertyValue
Are you using latest Umbraco version?
Are "Property value converters" enabled?
Thanks,
Alex
Hi Andreas,
You can do the following also:
You may also, want to add in some null checking to your logic to ensure it doesn't fall over.
Thanks
Craig
That did the trick! Thank you so much :)
Hi Craig
Will your code work if "Property value converters" will be enabled?
Thanks,
Alex
Hi Alex,
If PropertyConverters are enabled the replace the code above with this:
As the type returned would be of IPublishedContent.
Again, I would advise null checking the property before trying to assign it.
Thanks
Craig
is working on a reply...