I'm in the need for making some null checks. The code below fetches images from a mediapicker (categoryItemImages) and it works as intended... as long as the dataType HAS any images in it. If the data type is empty (wich can occur) my code breaks at 'content.GetValue' - i cannot even do a nullcheck at typedMultiMediaPicker. Can someone tell me how to return an empty JsonResult if my datatype is empty...? - tnx iin advance... :-)
public class CategoryItemsApiController : UmbracoApiController
{
//https://our.umbraco.com/forum/using-umbraco-and-getting-started/100600-nestcontent-get-data-is-always-in-surface-controller
//GET: /umbraco/api/categoryitemsapi/getcategoryitemimages?nodeId=1169
[HttpGet]
public JsonResult<IEnumerable<CategoryImages>> GetCategoryItemImages(int nodeId)
{
IContentService contentService = Services.ContentService;
var content = contentService.GetById(nodeId);
var typedMultiMediaPicker = content.GetValue("categoryItemImages").ToString();
List<string> tempMedia = typedMultiMediaPicker.Split(',').ToList();
var listCategoryItemImages = new List<CategoryImages>();
foreach (var item in tempMedia)
{
var mediaItems = Umbraco.Media(item);
int id = mediaItems.Id;
var categoryImage = new CategoryImages()
{
id = id,
udi = Udi.Create("media", Guid.Parse(mediaItems.Key.ToString())),
src = mediaItems.Url()
};
listCategoryItemImages.Add(categoryImage);
}
return Json<IEnumerable<CategoryImages>>(listCategoryItemImages.ToList());
}
}
You can check if a property has a value using content.HasValue("categoryImages") and then return Json<Enumerable.Empty<CategoryImages>>() as the result if it is. Maybe that would work?
Hi Robin.
Just a heads up: The ContentService will hit the database directly which could give you performance issues.
In your code snippet you are first accessing the node via the contentService (database call) using the nodeId, then you are resolving the image id:s from a property and then you are calling the UmbracoHelper (Umbraco.Media) to resolve those images.
In other words: You are resolving the content node from the database directly, but you are resolving the media items from the cache. Is there a reason for this setup or is it a mistake?
Instead of resolving the "content" item from the ContentService, could you use Umbraco.Content(nodeId) instead? That way all your communication will be with the cache, PLUS you will get an IPublishedContent instead of a IContent, and IPublishedContent will give you a HasValue() method, which will solve your original problem.
@Dennis Adolfi - You're no less than a genius! - off course I should'nt use the IContent as in the linked tutorial... - some times You can stare yourself bild on the most simple things... now my code looks like below, and works as intended...! :)
Hi5...!
public class CategoryItemsApiController : UmbracoApiController
{
//GET: /umbraco/api/categoryitemsapi/getcategoryitemimages?nodeId=1169
[HttpGet]
public JsonResult<IEnumerable<CategoryImages>> GetCategoryItemImages(int nodeId)
{
var node = Umbraco.Content(nodeId);
if (node.HasValue("categoryItemImages"))
{
var listCategoryItemImages = new List<CategoryImages>();
var typedMultiMediaPicker = node.Value<IEnumerable<IPublishedContent>>("categoryItemImages");
foreach (var item in typedMultiMediaPicker)
{
var categoryImage = new CategoryImages()
{
id = item.Id,
udi = Udi.Create("media", Guid.Parse(item.Key.ToString())),
src = item.Url()
};
listCategoryItemImages.Add(categoryImage);
}
return Json<IEnumerable<CategoryImages>>(listCategoryItemImages.ToList());
}
else {
return Json(Enumerable.Empty<CategoryImages>());
}
}
}
Need to return empty JsonResult
I'm in the need for making some null checks. The code below fetches images from a mediapicker (categoryItemImages) and it works as intended... as long as the dataType HAS any images in it. If the data type is empty (wich can occur) my code breaks at 'content.GetValue' - i cannot even do a nullcheck at typedMultiMediaPicker. Can someone tell me how to return an empty JsonResult if my datatype is empty...? - tnx iin advance... :-)
You can check if a property has a value using
content.HasValue("categoryImages")
and then returnJson<Enumerable.Empty<CategoryImages>>()
as the result if it is. Maybe that would work?The IContentService does'nt have a 'HasValue' property - I've allready tried that... :-)
Hi Robin. Just a heads up: The ContentService will hit the database directly which could give you performance issues.
In your code snippet you are first accessing the node via the contentService (database call) using the nodeId, then you are resolving the image id:s from a property and then you are calling the UmbracoHelper (Umbraco.Media) to resolve those images.
In other words: You are resolving the content node from the database directly, but you are resolving the media items from the cache. Is there a reason for this setup or is it a mistake?
Instead of resolving the "content" item from the ContentService, could you use Umbraco.Content(nodeId) instead? That way all your communication will be with the cache, PLUS you will get an IPublishedContent instead of a IContent, and IPublishedContent will give you a HasValue() method, which will solve your original problem.
Cheers.
@Dennis Adolfi - You're no less than a genius! - off course I should'nt use the IContent as in the linked tutorial... - some times You can stare yourself bild on the most simple things... now my code looks like below, and works as intended...! :)
Hi5...!
Awesome, glad I could help!!!
is working on a reply...