1

I need to create anonymous access link to files in my SharePoint online site by sending server relative URL as a parameter. http://sharepointfieldnotes.blogspot.sg/2016/01/whats-new-in-sharepoint-2016-remote-api_28.html mentions about a newly added method called createAnonymousLink but I do not know how to invoke this.

I can fetch the anonymous access urls by invoking

 https://siteName/_api/web/lists/GetByTitle('Documents')/items(1)/GetObjectSharingInformation?$expand=SharedWithUsersCollection&retrieveAnonymousLinks=true

I am consuming this REST API from a Java Application

Thanks

1 Answer 1

3

You can use it as below using REST api:

var ctx = SP.ClientContext.get_current();

 var url = _spPageContextInfo.webAbsoluteUrl + "/_api/SP.Web.CreateAnonymousLink";
          console.log(url);
          $.ajax(
          {
              'url': url,
              'method': 'POST',
              'data': JSON.stringify({
                 'url': _spPageContextInfo.webAbsoluteUrl + '/Shared Documents/test.txt',
                 'isEditLink': true
              }),
              'headers': {
              'accept': 'application/json;odata=verbose',
              'content-type': 'application/json;odata=verbose',
              'X-RequestDigest': $('#__REQUESTDIGEST').val()
              },
              'success': function (data) {
                    console.log(data.d.CreateAnonymousLink);
              },
              'error': function (err) {
                    console.log(JSON.stringify(err));
               }
          }
          );
         }

If you are using CSOM C#, you can use it as below:

// Create anonymous link for document
string link = ctx.Web.CreateAnonymousLinkForDocument(
                 "https://contoso.sharepoint.com/docs/sample.doc",
                 ExternalSharingDocumentOption.View);

if you want it to expire after certain days, it is as below:

// Create anonymous link with expiration for document
string link = ctx.Web.CreateAnonymousLinkWithExpirationForDocument(
                 "https://contoso.sharepoint.com/docs/sample.doc",
                 ExternalSharingDocumentOption.Edit,
                 DateTime.Now.AddDays(7));

Reference links:

External sharing API for SharePoint and OneDrive for Business

Rest Api Generate Guest Link SharePoint Online

PnP.Core.ExternalSharing

5
  • I need to use this API from a java application. I have edited my question to include this information
    – naresh
    Commented Oct 17, 2016 at 9:59
  • yes, so you can use the REST api call for invoking this method. replace, _spPageContextInfo.webAbsoluteUrl with your sharepoint site url Commented Oct 17, 2016 at 10:00
  • you can invoke this in Java, like you invoke any other POST request. I am not very familiar with Java coming from c# background, but you can use it like this i believe - stackoverflow.com/questions/4205980/… Commented Oct 17, 2016 at 10:15
  • @GautamSheth - Any idea why I cannot get the Rest API version working in Postman? Looks like maybe I am doing something wrong with the body payload formatting...? sharepoint.stackexchange.com/questions/254950/…
    – Tracy
    Commented Dec 28, 2018 at 17:24
  • @GautamSheth, do you know how to generate a sharing link for external users with authentication (anonymous links are too much of a security hazard for us)
    – Gaelle
    Commented Aug 25, 2022 at 15:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.