There are different ways to share a Site or Site content (Ex: List, Folder, File) with external users. In this post, we are going to explore how to add guest or external users to a SharePoint Online site, and how to invite guest users to a SharePoint document using CSOM based PowerShell script.
The below script assumes that you have already installed the Microsoft SharePoint Online SDK. The SharePoint Client library includes the two useful classes called WebSharingManager and DocumentSharingManager under the namespace Microsoft.SharePoint.Client.Sharing. We can use the class WebSharingManager to add external users to a SharePoint site and use the DocumentSharingManager class to share a specific document.
Summary
Add External User to a SharePoint Online site using PowerShell
We can use the UpdateWebSharingInformation method from CSOM client class WebSharingManager to share a site. The below script sends an invite to the given users and adds the users as a member of the Site Visitors group once they accepted the invitation. Run the below script after replacing the following parameters.
- $SiteURL – Provide URL of the site where to add users.
- $Users – Provide the email address of the required guest or external users as comma-separated values.
# Load required SharePoint client assemblies
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")
# Provide SharePoint Online Site URL
$SiteURL = "https://contoso.sharepoint.com/sites/site_name"
# Get user credentials to connect
$Cred = Get-Credential
# Users to share site
$Users = @("[email protected]","[email protected]")
$CustomMessage = "Please accept this invitation to access our Product site."
# Setup SharePoint site context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
# Create UserRoleAssignment List
$UserRoleAssignments = New-Object "System.Collections.Generic.List``1[Microsoft.SharePoint.Client.Sharing.UserRoleAssignment]"
# Add role assignment for each user
ForEach($User in $Users)
{
$RoleAssignment = New-Object Microsoft.SharePoint.Client.Sharing.UserRoleAssignment
$RoleAssignment.UserId = $User
# Set permission/role for the shared user. Roles: View, Edit and Owner
$RoleAssignment.Role = [Microsoft.SharePoint.Client.Sharing.Role]::View
$UserRoleAssignments.Add($RoleAssignment)
}
# Send external sharing request
$Result = [Microsoft.SharePoint.Client.Sharing.WebSharingManager]::UpdateWebSharingInformation($Ctx, $Ctx.Web, $UserRoleAssignments, $true, $CustomMessage, $true, $true)
$Ctx.ExecuteQuery()
# Display the sharing result
$Result | Select User,Status,IsUserKnown,InvitationLink,Message
Share a Document with External User using PowerShell
We can use the UpdateDocumentSharingInfo method from CSOM client class DocumentSharingManager to share an individual document file with external users. The below script sends an invite to the given users and provides view permission to the given document. Run the below script after replacing the following parameters.
- $SiteURL – Provide URL of the site where the file exists.
- $DocumentUrl – Full URL of the document to be shared. You can get the full path of the document from the Properties window (Details pane) in Document Library UI.(Ex: https://contoso.sharepoint.com/sites/site_name/shared%20documents/TestFile.docx).
- $Users – Provide the email address of the required guest or external users as comma-separated values.
# Load required SharePoint client assemblies
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")
# Specify SharePoint Online Site URL
$SiteURL = "https://contoso.sharepoint.com/sites/site_name"
# Set full path (URL) of the document to be shared.
$DocumentUrl = "https://contoso.sharepoint.com/sites/site_name/document_library/testfile.docx"
# Get user credentials to connect
$Cred = Get-Credential
# Users to share site
$Users = @("[email protected]","[email protected]")
$CustomMessage = "Please accept this invitation to access our document."
# Setup SharePoint site context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
# Create UserRoleAssignment List
$UserRoleAssignments = New-Object "System.Collections.Generic.List``1[Microsoft.SharePoint.Client.Sharing.UserRoleAssignment]"
# Add role assignment for each user
ForEach($User in $Users)
{
$RoleAssignment = New-Object Microsoft.SharePoint.Client.Sharing.UserRoleAssignment
$RoleAssignment.UserId = $User
#Set permission/role for the shared user. Roles: View, Edit and Owner
$RoleAssignment.Role = [Microsoft.SharePoint.Client.Sharing.Role]::View
$UserRoleAssignments.Add($RoleAssignment)
}
# Send document sharing invitation
$Result = [Microsoft.SharePoint.Client.Sharing.DocumentSharingManager]::UpdateDocumentSharingInfo($Ctx, $DocumentUrl, $UserRoleAssignments, $true,$true,$true, $CustomMessage, $true, $true)
$Ctx.ExecuteQuery()
# Display the sharing result
$Result | Select User,Status,IsUserKnown,InvitationLink,Message