Retrieve Images from Image Library in thumbnail format using Rest API
-
06-10-2020 - |
Вопрос
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?
Решение
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