Using Microsoft Graph To Become A Pdf Preview Of A File Inwards Sharepoint Past Times File Path

By Wednesday, July 25, 2018 on July 25, 2018 Tags:
Using Microsoft Graph To Become A Pdf Preview Of A File Inwards Sharepoint Past Times File Path.

The viewfinder of a photographic tv set camera shows a photograph of the sunset Using Microsoft Graph to become a PDF preview of a file inwards SharePoint past times file path
Photo past times Glenn Carstens-Peters on Unsplash

There are multiple ways to become a PDF version of a file, therefore I figured I’d present how yous via a path to a file inwards SharePoint tin utilisation the Microsoft Graph API to become a PDF version of that file. I’ll move using the Graph campaign detail conversion API for this.

A sample URL could hold off something similar this: https://contoso.sharepoint.com/sites/asite/FooLib/lala/Document.docx

[Update]

After posting the question on Stack Overflow I received an response from Vadim Gremyachev which takes it downward to 1 API call.

Basically he clued me onto how yous tin practice a sharing token for the detail URL which is genuinely the file id. Code for this is listed inwards the Graph Sharing API docs.

First yous base64 encode the URL, supercede about characters as well as prefix alongside u!, as well as therefore access the files via the /sharing API. The below code is using PowerShell to build the token.

$url = 'https://contoso.sharepoint.com/sites/asite/FooLib/lala/Document.docx' "u!"+[Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes($url)).TrimEnd('=').Replace('/','_').Replace('+','-')  u!aHR0cHM6Ly9jb250b3NvLnNoYXJlcG9pbnQuY29tL3NpdGVzL2FzaXRlL0Zvb0xpYi9sYWxhL0RvY3VtZW50LmRvY3g 

Armed alongside the token the number API telephone telephone is:

https://graph.microsoft.com/v1.0/shares/u!aHR0cHM6Ly9jb250b3NvLnNoYXJlcG9pbnQuY29tL3NpdGVzL2FzaXRlL0Zvb0xpYi9sYWxhL0RvY3VtZW50LmRvY3g/driveItem/content?format=pdf

[Original post]

In companionship to become to the actual file 2 API calls are needed, 1 to fetch the campaign (library) id, as well as 1 to fetch the file.

Note: This solution volition non tumble out the rootage site collection equally I brand assumptions on the number of parts of a URL. The next file formats are supported: csv, doc, docx, odp, ods, odt, pot, potm, potx, pps, ppsx, ppsxm, ppt, pptm, pptx, rtf, xls, xlsx.

Deconstructing the file URL

Splitting the URL on slashes nosotros become the parts needed to become the id of the document library as well as the id of the file.

0 https:
1
2 contoso.sharepoint.com
3 sites
4 pub
5 FooLib
6 lala
7 Document.docx

Part 2 is the tenant hostname, role 3+4 is the site path, role v is the document library, as well as role vi as well as out is the detail path relative to the document library.

Getting the campaign id (id of document library)

Using the sample URL inwards a higher house nosotros combine the sites as well as drives API’s inwards 1 query:

/v1.0/sites/{hostname}:{server-relative-path}:/drives  

resulting inwards the next query where nosotros select id as well as url

https://graph.microsoft.com/v1.0/sites/contos.sharepoint.com:/sites/asite:/drives?$select=id,weburl  

The output of this telephone telephone are all the libraries inwards the site.

{     "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#drives(id,webUrl)",     "value": [         {             "id": "b!H11aFSof8062NsPf4rr-qE3OKQpUIjVEp7PzqdeT_psgYyKuXH2VR7fGsvWPyBOt",             "webUrl": "https://contoso.sharepoint.com/sites/asite/Documents"         },         {             "id": "b!H11aFSof8062NsPf4rr-qE3OKQpUIjVEp7PzqdeT_pv8T5clDnpiRZq2uVmXgGRU",             "webUrl": "https://contoso.sharepoint.com/sites/asite/FooLib"         },         {             "id": "b!H11aFSof8062NsPf4rr-qE3OKQpUIjVEp7PzqdeT_psUQF8PSnx9T7aXwvRalLc_",             "webUrl": "https://contoso.sharepoint.com/sites/asite/PublishingImages"         },         {             "id": "b!H11aFSof8062NsPf4rr-qE3OKQpUIjVEp7PzqdeT_pv01hj6qcWyR5wulob7Lk7-",             "webUrl": "https://contoso.sharepoint.com/sites/asite/Pages"         },         {             "id": "b!H11aFSof8062NsPf4rr-qE3OKQpUIjVEp7PzqdeT_pvEaXdch-3DToEk0qR4g-xx",             "webUrl": "https://contoso.sharepoint.com/sites/asite/SiteCollectionDocuments"         },         {             "id": "b!H11aFSof8062NsPf4rr-qE3OKQpUIjVEp7PzqdeT_ptwBh2OaBQOTbJMXT5jLKwi",             "webUrl": "https://contoso.sharepoint.com/sites/asite/SiteCollectionImages"         },         {             "id": "b!H11aFSof8062NsPf4rr-qE3OKQpUIjVEp7PzqdeT_pv-q5N0D8gWSLB-0MY7_RS3",             "webUrl": "https://contoso.sharepoint.com/sites/asite/Translation%20Packages"         }     ] } 

Ideally yous would utilisation a $filter query to selection out only the library yous want, but this is non supported for the drives endpoint, therefore yous require to post-filter yourself.

By filtering out the detail which has a webUrl  matching role 2,3 as well as iv combined yous direct maintain the library yous are looking for.

Getting the PDF URL for the file

With the id of the document library inwards hand, it’s fourth dimension for the adjacent query which volition render the URL of the PDF version inwards a 302 Location header.

/v1.0/drives/{drive-id}/root:/{item-path}:/content?format=pdf  

Using the campaign id from the previous telephone telephone together alongside the document path I halt upwards alongside the next URL

https://graph.microsoft.com/v1.0/drives/b!H11aFSof8062NsPf4rr-qE3OKQpUIjVEp7PzqdeT_pv8T5clDnpiRZq2uVmXgGRU/root:/FooLib/lala/Document.docx:/content?format=pdf  

If yous hold off at the Location header inwards the returned response yous volition uncovering something similar to:

https://northeurope1-mediap.svc.ms/transform/pdf?provider=spo&inputFormat=docx&cs=N2FiNzg2….

This is a pre-authenticated URL which tin move called straight from anywhere without the require to logging in, as well as the URL is valid for a few minutes only.

Thanks for reading : Using Microsoft Graph To Become A Pdf Preview Of A File Inwards Sharepoint Past Times File Path


Related Post