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 :
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. 😦