Exploring Microsoft Graph API – Create shareable link for a specific user’s 

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.

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,

type – view / review / edit / embed / blocksDownload / createOnly / addresBar / adminDefault
scope – users / ( other properties are anonymous and organization )
expirationDateTime – optional
password – optional string
retainInheritedPermissions – If true, existing permissions are retained. If false, all existing permissions are removed
recipients – Add the collection of users who will receive the access to the shareable link
sendNotification – If true, send a sharing link in email to users mentioned in recipients

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.

In the recipient’s property, we have to set any of the below key-value pair,
email – User or Recipient’s email id
alias – Alias of the user
objectId – Unique identifier of the user / recipient.

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 
    } 
} 

Leave a Comment

Your email address will not be published. Required fields are marked *