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.
When the option is enabled, you generate the link from the share dialog box on a document.
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 :
https://tenant.sharepoint.com/_api/Web/GetFileByServerRelativeUrl('/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 :
https://tenant.sharepoint.com/_layouts/15/download.aspx?guestaccesstoken=lsRrsejmU9HlzY0Kw87A9NFu4yeDgMPusDBGF%2frz6AU%3d&docid=0588b11e4a047423e8667ee0ed9d67bf9&expiration=8%2f18%2f2015+9%3a14%3a34+AM&userid=28&authurl=True
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. 😦
Hi
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?
/Regards
Mike
LikeLiked by 1 person
That really helps me.
I’m still working on this using C#. Has anyone succeeded getting “External Shared” link by using SpFile.GetPreAuthorizedAccessUrl() method?
Please
LikeLike
How to create share link for Anonymous users for unlimited time for a SharePoint document using REST API?
LikeLike
Hi, using GetPreAuthorizedAccessUrl , it gives me link but with expired time eg. today is 2016-11-23 and it return expiration=2016-11-23T10%3a33%3a42.000Z and hence link is showing expired messages
Any Idea?
LikeLike
Did you find out how to stop sharing a link via the SP rest api?
LikeLike