2

I've uploaded a few images of kittens, teddy bears, flowers, and other cute things to a folder within the Sitecore Media Library. I now need to access them using the ItemService API and display them in my ASP.NET page.

When I access these images by their ItemID, I get 200 (OK) responses, which JSON string containing all the data pertaining to these images. But I'm unable to figure out a way how to display these images within my page. Some of the code snippets I am trying out are the following:

To access the ItemService API:

var client = new WebClient();
client.Headers.Add(HttpRequestHeader.ContentType, "image/jpeg");
// I am able to get an HTTP 200 response with the proper JSON with the following line
client.DownloadFile((@"https://<mysite>.azurewebsites.net/sitecore/api/ssc/item/<my item guid>", @"cutePicture.json");

I even try the DownloadData method of the WebClient class and attempt to convert the returned byte array to and Image using multiple ways, but each time I get errors saying 'Invalid Parameter' and others.

var client = new WebClient();
client.Headers.Add(HttpRequestHeader.ContentType, "image/jpeg");
var bytes = client.DownloadData(@"https://<mysite>.azurewebsites.net/sitecore/api/ssc/item/<my item guid>");
using (MemoryStream ms = new MemoryStream(bytes))
{
     Image myImage = Image.FromStream(ms); // Fails here
     myImage.Save(@"MyCuteImage.jpg");
}

I replace the using block with the following snippet, but that too doesn't let me convert the byte array to an image.

ImageConverter converter = new ImageConverter();
Image myImage = (Image)converter.ConvertFrom(bytes); // throws an error saying the parameter is invalid
myImage.Save(@"MyCuteImage.jpg");

Could someone please help me out here? Are there any settings/decorations I need to give my page to make Sitecore know that my page is accessing data from it? And how do I achieve my desired result, i.e., displaying a picture got from the Sitecore media library using the ItemService API onto my page? What am I missing here?

Any help with the above would be greatly appreciated!

Joonu

1
  • As you are using OData Item Service, take a look in this article -> doc.sitecore.net/sitecore_experience_platform/developing/… . You haven't specified version of Sitecore. Is Image__Url in returned json? Maybe use that parameter. Search for it in that article. That should help you out accessing images... If it's working I will convert this comment to answer with further clarification Aug 30, 2018 at 20:04

1 Answer 1

2

When you request a media item using ItemService API, You should receive the following field in the JSON respons:

ItemMedialUrl: "https://<mysite>.azurewebsites.net/-/media/kittens.ashx" 

You should be able to use that URL to show the image on your ASP.NET web page.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.