In SharePoint Online, sharing a file document or folder with other users is one of the important requirements for team collaboration. In this post, we will explore how to share a file document using Microsoft Graph API.
We can use the Sharing Invitation API to add permissions for internal users or share a drive item (file or folder) with external (or guest) users in SharePoint Online or OneDrive for Business site. We need an OAuth Access Token with the permission Files.ReadWrite.All or Sites.ReadWrite.All (Delegated or Application) to use this API. See this post: Register Azure AD App using Azure Portal and Get Access Token.
The below POST API request sends a sharing invitation to a user with the email address “[email protected]”. The user can be either an internal or external user. For an external user, a guest user account will be created in your Azure AD tenant when the user accepts the sharing invitation and access the file.
POST "https://graph.microsoft.com/v1.0/sites/{site-Id}/drive/items/{item-Id}/invite"
Content-type: application/json
{
"recipients": [
{
"email": "[email protected]"
}
],
"message": "File Shared with you. Please use this file to update",
"requireSignIn": true,
"sendInvitation": true,
"roles": [ "write" ]
}
The tricky part is proving a value for the input parameters {site-Id} and {item-Id}. For more details, see this post: How to Find Site Id, Drive Id and Item Id by using Microsoft Graph.
Steps to Add Permission or Share a File using Microsoft Graph API
Follow the below steps to share a file document in a SharePoint Online site library. Consider that you want to share a file with the below file URL.
https://contoso.sharepoint.com/sites/TestPTSite01/TestLibrary/TestFolder/test_file.txt
Step 1: Find Site Id by Site Name
Use the below API request to retrieve the site details by site name.
GET https://graph.microsoft.com/v1.0/sites?$search="TestPTSite01"
Step 2: Find Drive Id by Document Library name
Once you find the site id, use the following request to get all the drives (Document Library) available on the site. You can match the library name and find the drive id of the library.
GET https://graph.microsoft.com/v1.0/sites/{site-id}/drives
Step 3: Find Drive Item Id by File Path that Relative to Document Library
With the Site Id and Drive Id, we can easily find the drive item (List item) id by using the file path that is relative to the drive root (library).
#GET Request
https://graph.microsoft.com/v1.0/sites/{site-id}/drives/{drive-id}/root:/{path-relative-to-root}
#GET file details (item id)
https://graph.microsoft.com/v1.0/sites/{site-id}/drives/{drive-id}/root:/TestFolder/test_file.txt
Step 4: Send a sharing invitation
Finally, you can add permission to an internal user or share the file with an external user. The following POST API request sends a sharing invitation to a user with the email address “[email protected]”.
#POST API Request
https://graph.microsoft.com/v1.0/sites/{site-id}/drives/{drive-id}/items/{item-id}/invite
https://graph.microsoft.com/v1.0/sites/contoso.sharepoint.com,xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx,xxxxxxxx-xxxx-xxxx-xxxxxxxxxxxxxxxx/drives/b!exXXXXX5kUaYGeMudUb-iwLXXXXXXXXXXXXXXXXXXXXXXXgH2QLHQ7AP30BGzLxZ/items/items/01DWSWXNXXXXXXXXXXXXXJS5SMEH3BZJ5G/invite
Content-type: application/json
{
"recipients": [
{ "email": "[email protected]" }
],
"message": "Here's the file that we're collaborating on.",
"requireSignIn": true,
"sendInvitation": true,
"roles": [
"write"
]
}