0

Is there any rest API to fetch the read access shared link for a fresh file added to SharePoint using the rest API?

Below is the Create File Rest API

url: http://siteurl/_api/web/GetFolderByServerRelativeUrl('/FolderName')/Files/add(url='a.xlsx',overwrite=true) method: POST body: "Contents of file in raw plain text" Headers: Authorization: "Bearer " + accessToken X-RequestDigest: form digest value content-length:length of post body

Now the task is to get the shared read access link for a.xlsx

2
  • Sorry, Please clarify your question. If I understand correctly - You have added new file within rest api call and now you need to retrieve link to this file? If yes - please provide your call for creating the file.
    – Raf
    Dec 26, 2019 at 11:11
  • Apologies, edited the question Dec 26, 2019 at 12:08

1 Answer 1

0

We can use the REST API below to achieve it.

https://sitecollectionurl/_api/SP.Web.CreateAnonymousLink

Example code:

<script src="https://code.jquery.com/jquery-1.12.4.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function(){
    anonymousAccessLink();
});
function anonymousAccessLink(){
    var _selectedFileName = "a.xlsx";
    var url = _spPageContextInfo.webAbsoluteUrl + "/_api/SP.Web.CreateAnonymousLink";
    $.ajax({
        'url': url,
        'method': 'POST',
        'data': JSON.stringify({
            'url': _spPageContextInfo.webAbsoluteUrl + '/FolderName/' + _selectedFileName,
            'isEditLink': false
        }),
        'headers': {                                                                                                         
            'accept': 'application/json;odata=verbose',
            'content-type': 'application/json;odata=verbose',
            'X-RequestDigest': $('#__REQUESTDIGEST').val()
        },
        'success': function (data) {
            var resultData = data.d.CreateAnonymousLink;
            console.log(resultData);
        },
        'error': function (result, status) {                                                                                                 
            console.log("Ajax anonymous access :-"+JSON.stringify(result)+' - '+ status);
        }
    }); // End of ajax anonymous access.
}
</script>

Refer to: Rest Api Generate Guest Link SharePoint Online

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.