Create time-limited share link for a SharePoint document using REST API

If you use SharePoint to store your documents, you necessarily needed one day to share a document with an external user to your organization.

SharePoint, like other solutions such as OneDrive, DropBox, Box, GoogleDocs… gives you the capability to generate guest links on files stored in document libraries.

To be able to generate those kind of links, you must enable the correct option at the tenant level as you can see on the screenshot below.

External Sharing - Settings

When the option is enabled, you generate the link from the share dialog box on a document.

External Sharing - Guest Link

It’s very useful but as you can see, there’s no option to limit the time for which the link is available. When you want to disable the link, you have to go back to the dialog box of the document an select the “Disable” option. If you do this on hundreds or thousands of documents, it will be unmanageable very fast.

REST API to our rescue

Fortunately, as developers, there’s often a solution that we can use to cover our needs.

In our case, if you look into the REST API (but it’s also available with CSOM), on a SP.File object, there’s a method called GetPreAuthorizedAccessUrl which allows you to generate a time-limited link.

There’s many possibilities to get a SP.File object to then call the method, so let’s take an example using the GetFileByServerRelativeUrl from an SP.Web :'/Shared%20Documents/2013%20Contoso%20products.pdf')/GetPreAuthorizedAccessUrl(24)

As you can see above, we retrieve the SP.File object associated to a file stored in a library, then we generate the link which will be available for one day (24 hours). The response returned by the server is something like :

Now that you have a link which doesn’t require authentication to access/download the file, you just have to send it to your external users.

After one day, if they try to use the same link, they will receive an error message.

As we have seen in this article, SharePoint has all necessary things to generate time-limited links to share documents.

Unfortunately, there’s no option (at this time) in the UI to use this capability, so it’s up to you to develop it. 😦

5 thoughts on “Create time-limited share link for a SharePoint document using REST API

  1. zodor


    I have tried this (seems that not very many blogs is covering this particulary rest-call).
    I have managed to create the link, but the link does not work very well. It redirects me to the site’s startpage. Strange. Any idea what can be wrong here?


    Liked by 1 person

  2. cyfirewind

    That really helps me.
    I’m still working on this using C#. Has anyone succeeded getting “External Shared” link by using SpFile.GetPreAuthorizedAccessUrl() method?



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.