SharePoint Online: PowerShell to Change Access Request Email for All Sites
Requirement: Change Access Request Email in SharePoint Online.
PowerShell to Change Access Request Email in SharePoint Online for a Site Collection:
Let’s set access request email for a SharePoint Online site collection using PowerShell:
#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"
#Function to call a non-generic method Load
Function Invoke-LoadMethod() {
param([Microsoft.SharePoint.Client.ClientObject]$Object = $(throw "Please provide a Client Object"),[string]$PropertyName)
$ctx = $Object.Context
$load = [Microsoft.SharePoint.Client.ClientContext].GetMethod("Load")
$type = $Object.GetType()
$clientLoad = $load.MakeGenericMethod($type)
$Parameter = [System.Linq.Expressions.Expression]::Parameter(($type), $type.Name)
$Expression = [System.Linq.Expressions.Expression]::Lambda([System.Linq.Expressions.Expression]::Convert([System.Linq.Expressions.Expression]::PropertyOrField($Parameter,$PropertyName),[System.Object] ), $($Parameter))
$ExpressionArray = [System.Array]::CreateInstance($Expression.GetType(), 1)
$ExpressionArray.SetValue($Expression, 0)
$clientLoad.Invoke($ctx,@($Object,$ExpressionArray))
}
Function Set-SPOAccessRequestEmail([String]$SiteURL, [String]$AccessReqEmail)
{
Try{
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials
#Get the Web and Sub Sites
$Web = $Ctx.Web
$Ctx.Load($Web)
$Ctx.Load($web.AllProperties)
$Ctx.Load($Web.Webs)
$Ctx.ExecuteQuery()
#Check if the web has unique permissions
Invoke-LoadMethod -Object $Web -PropertyName "HasUniqueRoleAssignments"
$Ctx.ExecuteQuery()
If($Web.HasUniqueRoleAssignments -eq $true)
{
#Set Access request Email
$Web.RequestAccessEmail = $AccessReqEmail
$Web.Update()
$Ctx.ExecuteQuery()
Write-Host -f Green "Access Request Email Updated for the Site:" $($SiteURL)
}
Else
{
Write-Host -f Yellow "Site '$($SiteURL)' is inhering Permissions from the Parent"
}
#Iterate through each subsite of the current web
ForEach ($Subweb in $Ctx.Web.Webs)
{
#Call the function recursively
Set-SPOAccessRequestEmail -SiteURL $Subweb.url -AccessReqEmail $AccessReqEmail
}
}
Catch {
write-host -f Red "Error Updating Access Request Email!" $_.Exception.Message
}
}
#Set parameter values
$SiteURL="https://Crescent.sharepoint.com"
$AccessReqEmail="[email protected]"
#Get Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Call the function
Set-SPOAccessRequestEmail -SiteURL $SiteURL -AccessReqEmail $AccessReqEmail
SharePoint Online: Change Access Request Email Address for All Sites in the Tenant
Let me show you how to use a PowerShell script to change the email address for all SharePoint Online sites. This is a convenient way to manage your organization’s email addresses and keep them consistent across all sites.
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.Online.SharePoint.Client.Tenant.dll"
#Function to call a non-generic method Load
Function Invoke-LoadMethod() {
param([Microsoft.SharePoint.Client.ClientObject]$Object = $(throw "Please provide a Client Object"),[string]$PropertyName)
$ctx = $Object.Context
$load = [Microsoft.SharePoint.Client.ClientContext].GetMethod("Load")
$type = $Object.GetType()
$clientLoad = $load.MakeGenericMethod($type)
$Parameter = [System.Linq.Expressions.Expression]::Parameter(($type), $type.Name)
$Expression = [System.Linq.Expressions.Expression]::Lambda([System.Linq.Expressions.Expression]::Convert([System.Linq.Expressions.Expression]::PropertyOrField($Parameter,$PropertyName),[System.Object] ), $($Parameter))
$ExpressionArray = [System.Array]::CreateInstance($Expression.GetType(), 1)
$ExpressionArray.SetValue($Expression, 0)
$clientLoad.Invoke($ctx,@($Object,$ExpressionArray))
}
#Function to set access request Email of a site/site collection
Function Set-SPOAccessRequestEmail([String]$SiteURL, [String]$AccessReqEmail)
{
Try{
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials
#Get the Web and Sub Sites
$Web = $Ctx.Web
$Ctx.Load($Web)
$Ctx.Load($web.AllProperties)
$Ctx.Load($Web.Webs)
$Ctx.ExecuteQuery()
#Check if the web has unique permissions
Invoke-LoadMethod -Object $Web -PropertyName "HasUniqueRoleAssignments"
$Ctx.ExecuteQuery()
If($Web.HasUniqueRoleAssignments -eq $true)
{
#Set Access request Email
$Web.RequestAccessEmail = $AccessReqEmail
$Web.Update()
$Ctx.ExecuteQuery()
Write-Host -f Green "Access Request Email Updated for the Site:" $($SiteURL)
}
Else
{
Write-Host -f Yellow "Site '$($SiteURL)' is inhering Permissions from the Parent"
}
#Iterate through each subsite of the current web
ForEach ($Subweb in $Ctx.Web.Webs)
{
#Call the function recursively
Set-SPOAccessRequestEmail -SiteURL $Subweb.url -AccessReqEmail $AccessReqEmail
}
}
Catch {
write-host -f Red "Error Updating Access Request Email!" $_.Exception.Message
}
}
#Change Access Request Settings for All Site collections in the Tenant- Including Modern Team sites and communication sites
Function Change-SPOTenantAccessRequestEmail($AdminSiteURL)
{
#Setup credentials to connect
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($AdminSiteURL)
$Ctx.Credentials = $Credentials
#Get the tenant object
$Tenant = New-Object Microsoft.Online.SharePoint.TenantAdministration.Tenant($ctx)
#Get All Site Collections
$SiteCollections=$Tenant.GetSitePropertiesFromSharePoint(0,$true)
$Ctx.Load($SiteCollections)
$Ctx.ExecuteQuery()
#Iterate through Each site collection
ForEach($Site in $SiteCollections)
{
Set-SPOAccessRequestEmail -SiteUrl $Site.Url -AccessReqEmail $AccessReqEmail
}
}
#Set global Parameters
$AdminSiteUrl = "https://Crescent-admin.sharepoint.com"
$AccessReqEmail="[email protected]"
#Get Credentials to connect
$Cred= Get-Credential
#Call the function
Change-SPOTenantAccessRequestEmail -AdminSiteURL $AdminSiteUrl
This script changes access request email for all site collections in SharePoint Online.
Here is another post to change access request email or disable access request using PowerShell for a single SharePoint Online site: How to Change Access Request email or Disable Access Requests in SharePoint Online