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?

2 Answers 2

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
6
  • 2
    For getting file names you could utilize the following request: /_api/web/lists/getbytitle('Images')/items?$select=FileRef Commented Oct 23, 2015 at 7:27
  • 1
    Thanks a lot @Vadim, your answer lead me to the right track. Thanks again
    – Rahul
    Commented Oct 23, 2015 at 10:41
  • System.IO.Path.GetExtension(fileUrl) +".jpg" your are adding extension two times ? Commented Oct 31, 2018 at 7:38
  • 1
    No, it is System.IO.Path.GetFileNameWithoutExtension(fileUrl) Commented Oct 31, 2018 at 7:43
  • 1
    sorry. i got it wrong Commented Nov 1, 2018 at 12:51
0

SharePoint generates thumbnail file name using the following format:

thumbnailUrl = <fileFolder>/_t/<filename>_<fileExtension>.jpg 

So for the file MainLibrary/Tables/Table01.pptx it will be MainLibrary/Tables/_t/Table01_pptx.jpg

In my case I have url:

https://companyName.sharepoint.com/sites/projectName 

So I can get file by this link:

https://companyName.sharepoint.com/sites/projectName/_api/web/GetFileByUrl('MainLibrary/Tables/_t/Table01_pptx.jpg')/$value

Your Answer

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

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