SharePoint Online – Anonymous access link of a document programmatically using REST.
Introduction:
Hello techies, in today’s topic I am going to describe how we can generate an anonymous access link of a document programmatically using REST and JavaScript. To demonstrate, I am going to use a HTML file which will contains HTML controls and JavaScript/jQuery code, a web part page.
Prerequisites:
- External sharing should be enabled for the site collection.
- jQuery Library. I’m using “jquery-1.12.4.min.js”.
- Document library (with a test document). I am using a PDF file, you can test the same with other SharePoint online supported files types.
Let’s get to the steps…
Step – 1:
First, we must enable the external sharing for the Site Collection. If external sharing is not enabled, then you will get error message as below. Follow this link to know more about external sharing on SharePoint online.
Fig 1: Anonymous access link – Sharing by link error message
Step – 2:
Create document library and upload a PDF, text file or create word/excel document under it. In my case I’ve uploaded a PDF file named “Test_document.pdf” to the document library named “ReportPdfsRepository”.
Fig 2: Anonymous access link – Document library
Step – 3:
Now, we need create .html or .js file. It is not necessary to have HTML DOM/controls as it is a REST call and can be triggered on load, document ready, etc. but I am going to add one button and a span for demonstration purpose.
Code Snippet: Anonymous access link – HTML
<style type="text/css"> .ms-webpartPage-root {border-spacing: 0px !important;} .ms-webpartzone-cell {margin: 0px !important;} #txtUrl{ color: #333333; } #txtUrlError{ color:red;} </style> <table width="100%"> <tr> <td><h3>Anonymous Access Link:</h3></td> </tr> <tr> <td><span id="txtUrl"></span><br/><span id="txtUrlError"></span><br/></td> </tr> <tr> <td><button type="button" name="btnSendMl" id="btnSendMl" class="btn btn-primary">Get URL! </button></td> </tr> </table> <script src="https://code.jquery.com/jquery-1.12.4.min.js" type="text/javascript"></script>
Anonymous access link:
Microsoft have provided several endpoints such as /_api/web/lists/
,/_api/search/
, etc. Similarly, we have a REST endpoint for creating anonymous access link,
i.e. /_api/SP.Web.CreateAnonymousLink
By using this endpoint, we will be able to create link for the document. It’s important to note that the REST method should be “POST” and the data parameter should be in JSON format.
Code Snippet: Anonymous access link – REST call
// Function for creating anonymous access link var anonymousAccessLink = function(){ var _selectedFileName = "Test_document.pdf"; var url = _spPageContextInfo.webAbsoluteUrl + "/_api/SP.Web.CreateAnonymousLink"; $.ajax({ 'url': url, 'method': 'POST', 'data': JSON.stringify({ 'url': _spPageContextInfo.webAbsoluteUrl + '/ReportPdfsRepository/' + _selectedFileName, 'isEditLink': false }), 'headers': { 'accept': 'application/json;odata=verbose', 'content-type': 'application/json;odata=verbose', 'X-RequestDigest': $('#__REQUESTDIGEST').val() }, 'success': function (data) { resultData = data.d.CreateAnonymousLink; $("#txtUrlError").text(""); $("#txtUrl").text(resultData); }, 'error': function (result, status) { $("#txtUrl").text(""); $("#txtUrlError").text(status.toUpperCase()+" :-"+ result.responseJSON.error.message.value); console.log("Ajax anonymous access :-"+JSON.stringify(result)+' - '+ status); } }); // End of ajax anonymous access. }; // anonymousAccessLink()
Now, we have to call above function on page load, button click, document ready, etc. I’m using button click,
Code Snippet: Anonymous access link – Call the function
$(document).ready(function(){ $("#btnSendMl").on("click",function(){ SP.SOD.executeFunc('sp.js', 'SP.ClientContext', anonymousAccessLink); }); });
Step – 4:
- Upload this HTML/JS file to Site Asset.
- Create a web part page.
- Add a content editor web part to the web part page.
- Add the HTML/JS file link to the content editor web part.
Now, the button will be available on the page. Click on it and it will show the result as bellow.
Fig 3: Anonymous access link – Result
Thanks for reading 🙂
Keep reading, share your thoughts, experiences. Feel free to contact us to discuss more. If you have any suggestion / feedback / doubt, you are most welcome.
Stay tuned on Knowledge-Junction, will come up with more such articles
You must log in to post a comment.