SharePoint Online: Share Site and Invite External Users using PowerShell

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

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 = "Mark.Ellis@gmail.com","Sayeed.Alhajiri@gmail.com"
 
#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
SharePoint Online: Share Site and Invite External Users using PowerShell SharePoint Online: Share Site and Invite External Users using PowerShell Reviewed by Salaudeen Rajack on September 18, 2018 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.