SharePoint Online: Share Site and Invite External Users using PowerShell

Requirement: Share SharePoint Online Site with Multiple External Users using PowerShell.

How to Invite External Users to SharePoint Online?
Before adding external user to SharePoint Online site, make sure your site collection is configured to allow external user! You can quickly check if external sharing is enabled for your SharePoint tenant by going to:
  • SharePoint Admin Center, Expand Polices and then Sharing
  • In the "External Sharing", make sure the "SharePoint" settings are not set to "Only people in your organization". If its set to "Existing guests" only, then you must invite external user to your Azure AD first prior adding them to SharePoint Online.
    how to allow external users access to sharepoint online
Similarly, check if the external sharing is enabled at the site collection level by:
  • Login to SharePoint Admin Center, Expand Sites >> Active Sites
  • Select the target site from the list and click on "Sharing" button in the ribbon. 
  • In the Sharing panel, make sure its not set to "Only People in your Organization" (which means, External sharing is disabled!)
    how to invite external users to sharepoint online

How to Share a SharePoint Online Site with External Users?
Once you confirmed external sharing is enabled for your site, do the following to share the site with external users.
  • Navigate to your SharePoint Online site that you want to share with external users
  • Click on "Share" button on the top-right corner. Alternatively, you can click on Site Settings >> Site Permissions >> Share Site.
    how to share a sharepoint online site with external users
  • In the Share site panel, Enter the E-mail ID of the external user, Select the permission level such as "Read", Choose whether sharing invitation to be sent and then click on "Share" to add external user to SharePoint Online.
    how to provide access to external users in sharepoint online

SharePoint Online: Share with External Users using PowerShell
This PowerShell script invites external users to a SharePoint site and grants View permissions within the site. A custom e-mail message also included in invitation email.
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

#Set Variables
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
$Users = "[email protected]","[email protected]"
 
#Get Credentials to connect
$Cred = Get-Credential
 
Try {
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)

    #Create Request List
    $UserList = New-Object "System.Collections.Generic.List``1[Microsoft.SharePoint.Client.Sharing.UserRoleAssignment]"

    #Set Role for each user
    ForEach($User in $Users)
    {
        $UserRoleAssignment = New-Object Microsoft.SharePoint.Client.Sharing.UserRoleAssignment
        $UserRoleAssignment.UserId = $User
        $UserRoleAssignment.Role = [Microsoft.SharePoint.Client.Sharing.Role]::View  #Other Options: Edit, or Owner
        $UserList.Add($UserRoleAssignment)
    }

    #Send invites
    $EmailMessage = "Please accept this invite to our Marketing Site. Thanks!"
    [Microsoft.SharePoint.Client.Sharing.WebSharingManager]::UpdateWebSharingInformation($Ctx, $Ctx.Web, $UserList, $True, $EmailMessage, $True, $True)
    $Ctx.ExecuteQuery()
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}
This script sends invites to external users as:
SharePoint Online share with external users powershell

External User can click the link in the Email to access your SharePoint site. Please note, The user must have a Microsoft Live ID! They can create a Microsoft account from their Email, if its not created already. 

 

You can also invite an external user using SharePoint Online Management Shell cmdlet or PnP PowerShell! How to Add an External User to SharePoint Online using PowerShell?

2 comments:

  1. I've tried this multiple times and I receive no errors and no emails.
    Is there somewhere to check if invitations have been sent?

    ReplyDelete
    Replies
    1. Yes, Go to Site Settings >> " Access requests and invitations " to get all pending access requests and invitations.

      Delete

Please Login and comment to get your questions answered!

Powered by Blogger.