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 ?

SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
2,896 questions
SharePoint Workflow
SharePoint Workflow
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Workflow: An orchestrated and repeatable pattern of business activity, enabling data transformation, service provision, and information retrieval.
569 questions
SharePoint Server Development
SharePoint Server Development
SharePoint Server: A family of Microsoft on-premises document management and storage systems.Development: The process of researching, productizing, and refining new or existing technologies.
1,609 questions
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 34,661 Reputation points Microsoft Vendor
    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.