Yesterday on the Office 365 Yammer Network, someone asked if it was possible to add external users to a group using CSOM.
After a quick research, I found that it was a recurrent question and there’s no clear answer to that question. So I decided to investigate and I found the answer 🙂
In CSOM, there’s a namespace called Microsoft.SharePoint.Client.Sharing in which there are two useful classes called WebSharingManager and DocumentSharingManager.
These two classes are used to share a site or a document with users (internal or external).
In my case, I want to share a site with an external user so I need to use WebSharingManager.
This class has a method called UpdateWebSharingInformation with the following signature :
public static IList<UserSharingResult> UpdateWebSharingInformation( ClientRuntimeContext context, Web web, IList<UserRoleAssignment> userRoleAssignments, bool sendServerManagedNotification, string customMessage, bool additivePermission, bool allowExternalSharing )
As you can see, there are many parameters which are very easy to understand but the important parts are userRoleAssignments and allowExternalSharing.
The first is a collection of users with whom you want to share the resource. The second is to enable external sharing if the users are not employed by your company.
How to use it in practice ? It’s very easy.
using(var password = new SecureString()) { foreach(var c in "myPassword".ToCharArray()) password.AppendChar(c); using(var ctx = new ClientContext("https://tenant.sharepoint.com/")) { ctx.Credentials = new SharePointOnlineCredentials("user@tenant.onmicrosoft.com", password); var users = new List<UserRoleAssignment>(); users.Add(new UserRoleAssignment() { UserId = "username@externaldomain.com", Role = Role.View }); WebSharingManager.UpdateWebSharingInformation(ctx, ctx.Web, users, true, null, true, true); ctx.ExecuteQuery(); } }
When you execute the code above, the external user is invited as a viewer in the site associated to the CSOM context. He will receive an email to access to the site.
If you want to know if the invitation was sent successfully and if the user has accepted (or not), you just have to go to Access requests and invitations through the site settings.
If you want more information about WebSharingManager and DocumentSharingManager, just go to the official documentation on MSDN.
Pingback: SharePoint & Office 365: Recopilatorio de enlaces interesantes (III)! - Blog de Juan Carlos González en Geeks.MS
hi can you please provide a working powershell example thanks!
LikeLike
Can we Accept the request through code ?
LikeLike
Thanks for the post, hey do you know if there is a REST API for doing the same? We would like to do it thru SPD Worflow by calling the REST API (service) to add/share with external user.
LikeLike
Unfortunately that method WebSharingManager.UpdateWebSharingInformation is only available to use via CSOM or JSOM.
Below is a blog post explaining why the method can’t be used in REST, this is because it is expecting the parameter ‘web’
http://sharepointfieldnotes.blogspot.co.uk/2015/12/whats-new-in-sharepoint-2016-remote-api.html
LikeLike
can we share documents by this code ?
LikeLike
i also want to do.
LikeLike
The WebSharingManager class don’t seems to be present in Microsoft.SharePoint.Client download via nuget, any one have the same problem ?
LikeLike
This article was written 2 years ago… Maybe the class has been deprecated and removed since that date.
LikeLike
It’s in Microsoft.SharePoint.Client.Sharing 🙂
LikeLike
Can we make external users added to specific SharePoint group when sending invitaion
LikeLike
I have same question.. Did you get any way to do it? Please do tell
LikeLike
This code works beautifully, thank you!
Is it possible to share just a particular folder instead of access to the entire site?
LikeLike
Is it possible to add user in particular group along with external sharing?
LikeLike
Is there a way to share a folder with an external user such that only an access code is required? I’ve raised a more detailed question on StackOverflow https://stackoverflow.com/questions/56279481/how-to-share-a-sharepoint-folder-with-an-external-user-without-requiring-a-micro
LikeLike