SOLVED

How to get guestaccess URL of on item/file?

Occasional Contributor

I've been searching for an opportunity to get files and folders guestaccess URL using REST api in Sharepoint Online
Is there a way to get it using RESTapi or JSOM? 

I found an example authored by @Vesa Juvonen, where it done using CSOM: 

https://github.com/SharePoint/PnP/tree/master/Samples/Core.ExternalSharing/Core.ExternalSharingWeb

 

There's a notice that similar API is available through REST also, but there not much documentation for this... :S

Any advice on this subject would be super :)

4 Replies
best response confirmed by Mihkel Moorats (Occasional Contributor)
Solution

Hi Mihkel,

 

It's possible with the following web's methods:

- CreateAnonymousLink

- CreateAnonymousLinkWithExpiration

 

Some valuable details can be read here in Steve Curran's blog post.

Methods are available in REST API POST calls. Below is an example calling `CreateAnonymousLink` method with jQuery:

 

var methodUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/SP.Web.CreateAnonymousLink";
var documentUrl = spPageContextInfo.webAbsoluteUrl + '/DocumentLib/DocumentName.docx';

$.ajax({
    'url': methodUrl,
    'method': 'POST',
    'data': JSON.stringify({
        'url': documentUrl,
        '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(err);
    }
});

 

Thanks alot @Andrew Koltyakov!

 

That is exactly what I was looking for. Super!

 

Although I found these web method before, but I was assuming these are for creating these links. I did not expect these to return me existing links also.

 

Thanks again! :)

Hy Tracy,

 

Yes I got it working just fine. Although the suggested way to use "getShareLink" also created the link.

In my latest project I found best solution to use PnP-JS:

https://github.com/SharePoint/PnP-JS-Core/wiki/Working-With:-Sharing#getsharinginformation

 

"getSharingInformation" is the one that returns sharing info. But you need to use POST method instead of GET.

I hope you find my answer useful :)

Mihkel