Generate thumbnail of a document stored in SharePoint from its URL

In a previous article, we have seen how to generate a document thumbnail from its metadata (siteId, webId, uniqueId, docId…) retrieved from the search engine.

With these metadata, you just have to build and call an URL such as the following :

https://tenant.sharepoint.com/_layouts/15/getpreview.ashx?guidFile=<GUID>&guidSite=<GUID>&guidWeb=<GUID>&docid=<Int>&metadatatoken=<String>

Although easy to implement, this solution is not convenient every time, especially when you don’t use the search engine to retrieve data.

For example, when you use the Video REST API or the SharePoint REST API to manipulate list items, you don’t retrieve some of the required properties to generate the preview as described in our previous article.

One solution should be to query the search engine for the associated file to retrieve required metadata, then build and call the URL to generate the preview. This solution works very well but it’s pretty inefficient because you have to make 2 requests for each thumbnail.

A better solution based on the same HTTP Handler

Last week, I was working on different things around Office 365 and I discovered a better solution based on the same HTTP Handler.

It’s possible, rather than passing many parameters (siteId, webId, docId…), to pass only one parameter called ‘path’.

This parameter expects a value equal to the URL of the document/file for which you want to generate a thumbnail. To generate a preview, the URL should look like this :

https://tenant.sharepoint.com/portals/hub/_layouts/15/getpreview.ashx?path=https%3A%2F%2Ftenant.sharepoint.com%2Fportals%2Fcommunity%2FpVid%2FBiking%2520to%2520Work.mp4

In the sample above which generates a thumbnail for a video stored in an Office 365 video channel), you just have to encode the path of the file, and add it to the querystring for the ‘path‘ parameter.

I have tested with many file types (Word, Excel, PowerPoint, MP4…), stored in different containers (document libraries, video channels…) and it works for each of them.

For information, this solution is used by Delve to generate thumbnails for video files stored in Office 365 Video 😉

Delve - HomePage

Advertisements

7 thoughts on “Generate thumbnail of a document stored in SharePoint from its URL

  1. Pingback: How to know what file types are supported to generate previews on Office 365 ? | Beecome Digital

  2. Pingback: CSOM GetImagePreviewUrl Parameters | Question and Answer

  3. Murthy VVR

    is this still working? I’m getting 400 bad request, when I tried for a document which is in a Document Library.

    Like

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s