I want to create a link using SharePoint CSOM API so anonymous user can read/edit file.
I found the following workaround to get it via adding role assignment:
var newRoleAssignment = new UserRoleAssignment() { Role = Role.Edit, UserId = userId };
DocumentSharingManager.UpdateDocumentSharingInfo(
clientContext,
absoluteFileUrl,
new List<UserRoleAssignment>() { newRoleAssignment },
validateExistingPermissions: true,
additiveMode: true,
sendServerManagedNotification: true,
customMessage: null,
includeAnonymousLinksInNotification: true);
var item = rootDocumentList.GetItemById(fileId);
var sharingInfo = ObjectSharingInformation.GetObjectSharingInformation(
clientContext, item, false, true, false, true, true, true, true);
clientContext.Load(sharingInfo);
clientContext.ExecuteQuery();
// then I can use sharingInfo.AnonymousEditLink
Is there a better way of doing this? This feels like a dirty hack, and I have no means to disable generated links, as non-admin user cannot remove role assigment.