1

I am using SharePoint 2013 rest api. I want to share SharePoint document among multiple users Here is my rest endpoint

https://XXXXXX.sharepoint.com/sites/XXXX-Test-SharePoint/_api/web/Lists(%27%7BB17B74B2-6EB4-4D3E-9DD6-4CCD0202C30D%7D%27)/GetItemById(90)/ShareObject

and passing POST data to endpoint as below

{"peoplePickerInput":[{"Key":"i:0#.f|membership|[email protected]","Description":"[email protected]","DisplayText":"John Miller ","EntityType":"User","ProviderDisplayName":"Tenant","ProviderName":"Tenant","IsResolved":true,"EntityData":{"IsAltSecIdPresent":false,"Title":"Partner","Email":"[email protected]","MobilePhone":0,"ObjectId":"c8dd8ac6-fb06-4d59-94e0-6e525b98146d","Department":"Professional Services Americas"},"MultipleMatches":[]},{"Key":"i:0#.f|membership|[email protected]","Description":"[email protected]","DisplayText":"david Miller ","EntityType":"User","ProviderDisplayName":"Tenant","ProviderName":"Tenant","IsResolved":true,"EntityData":{"IsAltSecIdPresent":false,"Title":"Partner","Email":"[email protected]","MobilePhone":0,"ObjectId":"c8dd8ac6-fb06-4d59-94e0-6e525b98146d","Department":"Professional Services Americas"},"MultipleMatches":[]}],"propagateAcl":true,"roleValue":"role:1073741827","sendEmail":true,"useSimplifiedRoles":true,"emailSubject":"Sharing Test","emailBody":"Shared you folder..","includeAnonymousLinkInEmail":false}

After executing above url, i am getting below response

{"odata.error":{"code":"-1, Microsoft.Data.OData.ODataException","message":{"lang":"en-US","value":"A node of type 'StartArray' was read from the JSON reader when trying to read a value of a property; however, a 'PrimitiveValue' or 'StartObject' node was expected."}}}

1 Answer 1

2

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': 'http://basesmc15/Shared%20Documents/A1210251607172880165.pdf',
            '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) {
            alert(JSON.stringify(err));
        }
    }
    );

}

ResourceAddress: This is the full URL to the document you want to share

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. The UserId property can be the name of the user or a role. For example, if you wanted to share the document with the “Translation Mangers” role and the “Steve Tester” user you would use this JSON:

'userRoleAssignments': [{
                '__metadata': {
                    'type': 'SP.Sharing.UserRoleAssignment'
                },
                'Role': 1,
                'UserId': 'Translation Managers'
            },
            {
                '__metadata': {
                    'type': 'SP.Sharing.UserRoleAssignment'
                },
                'Role': 1,
                'UserId': 'Steve Tester'
            }]

Sharing Documents with the SharePoint REST API by Steve Curran

1
  • Above code works for me but is it possible to share document with groups? Commented Jan 3, 2020 at 4:50

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.