4

I am trying to retrieve Images from Image Library using Rest API from a mobile device (hence, no cross site calls)..

I am using this approach (https://stackoverflow.com/questions/16893729/how-to-retrieve-image-files-from-sharepoint-image-library-list-through-rest-ap ) to get file binaries directly, not the server relative URL to avoid cross site requests.

I am able to get the images, however since I want to create a picture gallery on my phone, I want to download thumbnails first and then full image once you click on thumbnail.

Is there any way to get thumbnails using Rest API?

6

It seems SharePoint generates thumbnail file name using the following format:

thumbnailUrl = <filename>_<fileextension>.jpg 

Based on that, the following REST endpoint shows to get thumbnail file content:

/_api/web/getfilebyserverrelativeurl('{thumbnailUrl}')/$value

Example

var webUrl = "https://tenant.sharepoint.com";
var fileUrl = "/PublishingImages/sample.png";

var client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);

var requestUrl = String.Format("{0}/_api/web/getfilebyserverrelativeurl('{1}')/$value",webUrl,GetThumbnailUrl(fileUrl));
var response = client.GetAsync(requestUrl, HttpCompletionOption.ResponseHeadersRead).Result;
var fileContent = response.Content.ReadAsByteArrayAsync().Result;

where

public static string GetThumbnailUrl(string fileUrl)
{
    return System.IO.Path.GetFileNameWithoutExtension(fileUrl) + "_" + System.IO.Path.GetExtension(fileUrl) +".jpg";
}

Update

Probably the better option would be to get image thumbnail url via EncodedAbsThumbnailUrl property:

  • Get image thumbnail url using request: /_api/web/lists/getbytitle('<image library title>')/items(<item id>)?$select=EncodedAbsThumbnailUrl
  • then request thumbnail file content: /_api/web/getfilebyserverrelativeurl('{thumbnailUrl}')/$value
  • 1
    For getting file names you could utilize the following request: /_api/web/lists/getbytitle('Images')/items?$select=FileRef – Vadim Gremyachev Oct 23 '15 at 7:27
  • 1
    Thanks a lot @Vadim, your answer lead me to the right track. Thanks again – Rahul Patil Oct 23 '15 at 10:41
  • System.IO.Path.GetExtension(fileUrl) +".jpg" your are adding extension two times ? – Ranadheer Reddy Oct 31 '18 at 7:38
  • 1
    No, it is System.IO.Path.GetFileNameWithoutExtension(fileUrl) – Vadim Gremyachev Oct 31 '18 at 7:43
  • 1
    sorry. i got it wrong – Ranadheer Reddy Nov 1 '18 at 12:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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