How to create a shared link with an expiration date for the item of OneDrive For Business via Rest API?

Today it’s very convenient to share files stored in OneDrive For Business via shared links. Some developers integrate their apps with OneDrive For Business, they used to create a shared link via rest API. But sometimes we just want the shared link temporary, Is it possible for us to set an expiration date for the shared link?

MS Graph api did not document such an api that could achieve this goal, but finally we get the right api by tracing OneDrive website. So the answer is yes! Let’s firstly have a look at OneDrive website then manually set up a shared link.

Only when you select “Anyone” option, it will prompt a date pop-window. Some people may find that “Anyone” is gray and unselectable at their tenant. That is because this option is by default disabled. How can we enable this option?

Enable external sharing for SharePoint

We can configure this option in Office 365 admin center like below:

Of course, for the private site collection, you can set up it in sharepoint admin center. Additionally PowerShell is also available for the configuration.

$adminUPN="replace_with_username" $orgName="name_of_sharepoint_prefix" $usercredential = Get-Credential -username $adminUPN -Message "type password" Connect-SPOService -Url https://$ -Credential $userCredential

#Identify and connect to the desired Site Collection

#Enable External User and Guest Sharing on that chosen Site Collection

set-sposite -identity $Siteurl -sharingcapability ExternalUserAndGuestSharing

That’s it.  Now check your work (refresh the page/site collection), “Anyone” option should be available.

After that we are able to create an anonymous shared link with an expiration date Programmatically. Let’s investigate the relevant API.

  • Register an app and assign the below permission
  • Get through authentication procedure and acquire access token, you can refer to ADAL
  • Invoke the API

POST    https://{tenant}{site}/_api/Web/Lists/GetByTitle(‘Documents’)/Items(1)/sharelink

Body :  {“request”:{“createLink”:true,”settings”:{“linkKind”:5,”expiration”:”20180504T000000+0800″}}}

The above API creates a shared link on the item of “Document” library. And below is the response:

Please note that only the anonymous shared link supports expiration date. The other options currently have not released such an API or feature.


Now we are able to achieve the goal of setting up an shared link with an expiration date for OneDrive For Business. It will helps when you try to integrate your app with onedrive If you have anything unclear regarding this blog, please feel free to contact our team:

Leave a Reply

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