Office Graph API: Creating a New Document Based on a Template

calendar_month May 15, 2020

Recently I wanted to create a Word document based on a template using the Office Graph API and set a few metadata fields on the associated SharePoint ListItem. It turned out that it’s not a “straight-forward” job, which ultimately led to this article.

Problem in a Nutshell

I have a library where documents based on a ContentType AkDocumentDoc should be created. The said ContentType is of type Document and has a few additional columns.

AkDocumentDoc has a Word template attached, which means that when creating an element based on AkDocumentDoc, a Word document with this template is created at the same time. The template is called Pruef.dotx.

Now a logic – triggered however and wherever – based on this ContentType should create new elements in this library and set the metadata of the associated SharePoint ListItem.

All requests in this article can be sent either via the Graph Explorer or via Postman, for example. For the latter, the OAuth2 token must first be obtained and added to each query.

The Environment

For this example I use a fictitious O365 subscription. The following variables are needed:

NameExample ValueDescription
hostnamekirmizient.sharepoint.comSharePoint Tenant
siteUrlhttps://…/sites/halklailiskilerSiteCollection
graph siteIdhostname,siteCollectionId,siteIdCombined
graph drive idb!EWACgmr6…The Drive ID of the library

Finding the Site ID

Via the Search API:

https://graph.microsoft.com/v1.0/sites?search=halklailiskiler

The returned ID has the format: <hostname>,<siteCollectionId>,<siteId>.

Finding the Drive ID

https://graph.microsoft.com/v1.0/sites/<graph siteId>/drives

Creating a Document (OneDrive API)

Documents are created via a PUT request to the OneDrive API:

https://graph.microsoft.com/v1.0/sites/<graph siteId>/drives/<graph drive id>/root:/<filename.docx>:/content

The request header Content-Type: text/plain must be set.

Finding the Document (SharePoint API)

To successfully set the metadata of our document, we need to get to our document via the SharePoint API. Unfortunately, the response from the OneDrive API during/after creation doesn’t provide a usable value.

This GET request gets us the required ListItem ID:

https://graph.microsoft.com/v1.0/sites/<graph siteId>/lists/<listId>/items?select=id&filter=fields/FileLeafRef eq '<filename>.<file extension>'

Important: In the header include Prefer: HonorNonIndexedQueriesWarningMayFailRandomly, as FileLeafRef is not indexed.

Updating the Document (SharePoint API)

Now we can update the metadata of our document – the PATCH method is used for this:

PATCH https://graph.microsoft.com/v1.0/sites/<graph siteId>/lists/<listId>/items/<itemId>
{
  "fields": {
    "Title": "Test-Blog-AK",
    "FileLeafRef": "akblogtest123-update.docx",
    "ContentType": "AkDocumentDoc",
    "Test1": true,
    "Test2": false
  }
}

Summary

The document is created via the OneDrive API, filling in the metadata is done via the SharePoint API. There is no “straight-forward” way – that’s what I wanted to show with this article.