Sharepoint REST API - share document

Alvin 1 Reputation point
2021-08-15T08:32:22.847+00:00

Hi,

Does anyone knows how to use REST API to share a document in sharepoint to someone inside the same organization and external user ?

Microsoft 365 and Office SharePoint Development
Microsoft 365 and Office SharePoint For business Windows
Microsoft 365 and Office SharePoint Server Development
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Cesare Vesdani 11 Reputation points
    2021-08-15T21:43:07.877+00:00

    I don't know.

    0 comments No comments

  2. RaytheonXie_MSFT 40,471 Reputation points Microsoft External Staff
    2021-08-16T06:26:25.17+00:00

    Hi @Alvin ,
    Below is the REST call using JavaScript code that shares a document from a SharePoint hosted app:

    function shareDocument()  
    {  
        var hostweburl = decodeURIComponent(getQueryStringParameter('SPHostUrl'));  
        var appweburl = decodeURIComponent(getQueryStringParameter('SPAppWebUrl'));  
        var restSource = appweburl + "/_api/SP.Sharing.DocumentSharingManager.UpdateDocumentSharingInfo";  
       
    $.ajax(  
    {  
        'url': restSource,  
        'method': 'POST',  
        'data': JSON.stringify({  
            'resourceAddress': 'documenturl',  
            'userRoleAssignments': [{  
                '__metadata': {  
                    'type': 'SP.Sharing.UserRoleAssignment'  
                },  
                'Role': 1,  
                'UserId': 'Chris Tester'  
            }],  
            'validateExistingPermissions': false,  
            'additiveMode': true,  
            'sendServerManagedNotification': false,  
            'customMessage': "Please look at the following document",  
            'includeAnonymousLinksInNotification': false  
        }),  
        'headers': {  
            'accept': 'application/json;odata=verbose',  
            'content-type': 'application/json;odata=verbose',  
            'X-RequestDigest': $('#__REQUESTDIGEST').val()  
        },  
        'success': function (data) {  
            var d = data;  
        },  
        'error': function (err) {  
              
        }  
    }  
    );  
    }  
    

    UserRoleAssignments: This an array of users and roles that you want to share the document with. The Role property represents which permission you are assigning. 1 = View, 2 = Edit, 3 = Owner, 0 = None.


    If an Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.