Search documents and generate a preview with Office 365 REST API

Since Delve and Office Graph were announced last year, you may have asked how it’s working and if it’s possible to retrieve documents from all your SharePoint sites, and not only content from Delve/Office Graph.

Delve - HomePage

In the rest of this article, we are not going be focused on Delve / Office Graph (maybe in a future post) but we’re going to explain how to search any kinds of documents and how to generate a preview for these.

If you’re not familiar with Office 365 REST API, you are able to perform searches on content stored in your SharePoint sites by using the search endpoint.

https://<tenant>.sharepoint.com/_api/search

This endpoint offers 2 different possibilities to submit a query to the SharePoint search index.

For the first one, you just need to submit an HTTP GET request to URL below with some parameters in the querystring.

https://<tenant>.sharepoint.com/_api/search/query?querytext='OneDrive'

This first usage is described by a lot of blogs thus, do a search with Google/Bing if you want more information on this.

The second possibility is to submit an HTTP POST request to :

https://<tenant>.sharepoint.com/_api/search/postquery/

This second choice is more flexible if your search query is complex or very long (remember that an URL cannot exceed ~4000 characters).

Below you can see and example of the body you have to include in your HTTP POST message. (Important : SharePoint is case sensitive when you submit your search query).

{
    request =     {
        Querytext = "(ContentTypeId:0x010100F3754F12A9B6490D9622A01FE9D8F012*)",
        RowLimit = 20,
        SelectProperties =         (
            DocId,
            DefaultEncodingURL,
            DocumentPreviewMetadata,
            SiteId,
            Title,
            UniqueId,
            WebId
        ),
        StartRow = 0,
        TrimDuplicates = True
    }
}

This query will return the first 20 documents from Office 365 Video (based on the ContentTypeId). We only select properties needed (DocId, SiteId, Title…) and want to exclude duplicates.

The StartRow parameter allows you to perform paging if necessary.

There are many more kinds of parameters you can pass to your search query. Refer to the following MSDN page if you need more information : https://msdn.microsoft.com/en-us/library/office/jj163876.aspx

Now that we are able to retrieve a list of documents (Office 365 videos in our case), how to generate a preview for each of them ?

By analyzing how Delve is working, you can see that thumbnails are generated by calling an URL with the following format :

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

As you can see, these parameters are similar to those we have put in our search query :

  • guidFile = UniqueId
  • guidSite = SiteId
  • guidWeb = WebId
  • docId = DocId

But what about the metadatatoken parameter ? That’s the magic because we have put a particular property named DocumentPreviewMetadata in our search query.

If you look at the search result response, this property is returned in the following format.

{
    Key = DocumentPreviewMetadata,
    Value = "{\"previews\":[{\"h\":187,\"src\":0,\"w\":300}],\"token\":\"300x187x0\"}",
    ValueType = "Edm.String"
}

As you can see, the value for the property is a JSON serialized object in which you are able to get the width, the height and the token associated to the file preview. If the file has multiple preview (e.g. many slides in a PowerPoint document), the previews array will contain multiple values.

If you deserialize this value and put the token value into the metadatatoken parameter of the URL mentioned previously, you have all the items necessary to call the URL and to generate the preview for all your documents.

Thanks for reading this article and I hope it will help you to develop awesome apps by using the Office 365 REST API 😉

Advertisements

3 thoughts on “Search documents and generate a preview with Office 365 REST API

  1. Pingback: Office 365 Developer Podcast: Episode 037 on ng-conf and Angular with Office 365 development | POKORNY

  2. Pingback: Generate thumbnail of a document stored in SharePoint from its URL | Beecome Digital

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

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