11

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.

user2017507
  • 111
  • 3

1 Answers1

4

First question :

As far as I know this is the only way using the client model.

I didn't understand your solution at first, so here is what I think is happening : adding UserRoleAssignements using UpdateDocumentSharingInfo with the options you chose will trigger a mail notification. If you set includeAnonymousLinksInNotification to True, the method will also generate an anonymous link as a side effect, and include it in the mail.

Then, you can retrieve the link in the code using ObjectSharingInformation's GetObjectSharingInformation.

Too bad sharingInfo doesn't allow to create the edit or view links directly...

Second Question :

I don't think removing the role assignement would disable the link. From what i understood, when you create an anonymous link, a hidden guest user is created. (see slide 26 here : http://video.ch9.ms/sessions/spc/2012/SPC183_Doshi.pptx). I don't think removing the assignment will delete the hidden user or the generated link. Maybe if you delete the assignment with the hidden user ?? I wouldn't bet on it though.

I wish I could upvote your question, but I don't have any rep on Sharepoint@stackexchange.

misterfrb
  • 141
  • 3
  • any idea what the userID should be? I tried my personal account's user ID.. I tried to leave it as empty string "".. In all cases, it goes past the UpdateDocumentSharingInfo and then returns an Unknown Error exception when loading the sharingInfo object. – Jurgen Cuschieri May 16 '17 at 13:33
  • I use context.Web.CurrentUser.UserId.NameId I once had a weird exception when loading the sharingInfo object, I solved it by relogging in. Does your sharepoint site allow guest links ? Does your user have the permission to create them ? – misterfrb May 22 '17 at 15:39