question

Alvin-6448 avatar image
0 Votes"
Alvin-6448 asked RaytheonXie-MSFT commented

Sharepoint REST API - share document

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-devoffice-sharepoint-server-developmentsharepoint-workflow
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

CesareVesdani-8655 avatar image
0 Votes"
CesareVesdani-8655 answered

I don't know.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RaytheonXie-MSFT avatar image
0 Votes"
RaytheonXie-MSFT answered RaytheonXie-MSFT commented

Hi @Alvin-6448 ,
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.


· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Dear @RaytheonXie-MSFT

Thank you for the reply. The sample you provided on above is using Azure active directory user ?As I'm not using Azure AD, what if I want to share the document with someone with only email address ? Is that possible ?

0 Votes 0 ·

Hi @Alvin-6448 ,
As far as I know, we are unable to share document with email address. You can try csom to share document with users inside the same organization or you can use restapi grant users permission.Here is the sample for csom:

  var users = new List<string>() { "user1", "user2 " };
  var userRoles = new List<UserRoleAssignment>();
  foreach (var user in users)
  {
      clientContext.Web.EnsureUser(user);
      UserRoleAssignment role = new UserRoleAssignment();
      role.UserId = user;
      role.Role = Role.View; //Role.Edit
      userRoles.Add(role);
  }
  string absoluteFileUrl = "folderurl";
  DocumentSharingManager.UpdateDocumentSharingInfo(clientContext, absoluteFileUrl, userRoles, true, true, true, "Test Folder", true, true);
  clientContext.ExecuteQuery();
  Console.WriteLine("success");
  Console.ReadLine();


1 Vote 1 ·

Hi @Alvin-6448 ,
would you please provide us with an update on the status of your issue?

0 Votes 0 ·