Hi,
Does anyone knows how to use REST API to share a document in sharepoint to someone inside the same organization and external user ?
Hi,
Does anyone knows how to use REST API to share a document in sharepoint to someone inside the same organization and external user ?
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.
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 ?
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();
Hi @Alvin-6448 ,
would you please provide us with an update on the status of your issue?
6 people are following this question.
update modified by(user type field) and security classification (taxonomy filed) using rest API
SharePoint Rest API modify "who modified by" and security classification using OAuth token postman
Workflow not moving to next stage when task is assigned to a group
Hi guys, I need some help with the syntax to create a calculated column in SharePoint.