Microsoft Graph API comes in two versions. They are beta and v1.0. Microsoft mostly recommend us to use v1.0 for production uses. Beta represents a preview mode and any way, most of the properties and methods will make it to the v1.0 version.
Let’s come to point.
In this blog, I would like to show you on how to create a shareable link for certain users.
This feature is currently available in v1.0 version and works in SharePoint Online & OneDrive for Business services.
POST https://graph.microsoft.com/v1.0/drives/{driveId}/items/{itemId}/createLink
POST https://graph.microsoft.com/v1.0 /groups/{groupId}/drive/items/{itemId}/createLink
POST https://graph.microsoft.com/v1.0 /me/drive/items/{itemId}/createLink
POST https://graph.microsoft.com/v1.0 /sites/{siteId}/drive/items/{itemId}/createLink
POST https://graph.microsoft.com/v1.0 /users/{userId}/drive/items/{itemId}/createLink
To send the request for creating a Sharable link; We can add the below properties to body of the request along with the Graph API,
recipients and sendNotification properties are available in beta version as of today.
Scope and recipients are the important and required properties for setting the shareable link to the user.
Below is the example request sent from Microsoft Graph Explorer
Method: POST
Rest API: https://graph.microsoft.com/beta/me/drive/items/017VJESBOLHLZESLGEC5GJVHFEXEQLTSLC/createLink
Request Body:
{
"type": "view",
"scope": "users",
"recipients": [
{
"objectId": "14a91672-8033-4175-8737-1eab8519c40d"
}
]
} Request headers: (No need if we used Graph Explorer)
Authorization: Bearer {token}
Content-Type: application/json
The user who runs the code should have any of the below permissions,
Files.ReaWrite, Files.ReadWrite.All, Sites.ReadWrite.All,
The output response looks like below,
{
"@odata.context": "https://graph.microsoft.com/v1.0/$metadata#permission",
"@odata.type": "#microsoft.graph.permission",
"id": "91c58321-6166-44db-acda-91d4122d55f7",
"roles": [
"read"
],
"hasPassword": false,
"grantedToIdentitiesV2": [
{
"user": {
"@odata.type": "#microsoft.graph.sharePointIdentity",
"displayName": "User 1",
"email": "user1@contoso.onmicrosoft.com",
"id": "14a91672-8033-4175-8737-1eab8519c40d"
},
"siteUser": {
"displayName": "User 1",
"email": "user1@contoso.onmicrosoft.com",
"id": "9",
"loginName": "i:0#.f|membership|user1@contoso.onmicrosoft.com"
}
}
],
"grantedToIdentities": [
{
"user": {
"displayName": "User 1",
"email": "user1@contoso.onmicrosoft.com",
"id": "14a91672-8033-4175-8737-1eab8519c40d"
}
}
],
"link": {
"scope": "users",
"type": "view",
"webUrl": "https://contoso-my.sharepoint.com/:x:/g/personal/user2_contoso_onmicrosoft_com/Ecs68kksxBdMmpykuSC5yWIBvMcfS15-pf5zheFV9RFk9A?email=user1%40contoso.onmicrosoft.com",
"preventsDownload": false
}
}