SharePoint Online: Disable Access Requests for All Sites using PowerShell
Requirement: Disable access requests for SharePoint Online sites.
How to Disable Access Requests in SharePoint Online?
You may want to disable access requests in SharePoint Online in specific scenarios. We can disable the ability for people to request access to your SharePoint Online site, either from site settings or by using PowerShell. In this blog post, we’ll walk through how to use PowerShell to disable (or enable) user access requests in SharePoint Online. Let’s get started!
To disable access requests for a SharePoint Online site, do the following:
- Go to the SharePoint Online site where you would like to disable access requests.
- Click on the Settings gear icon in the upper-right corner, and select “Site Permissions” in the drop-down menu.
- On the Site Permissions panel, click on the “Change how members can share” link under “Site Sharing”.
- Switch the “Access Request” settings to “OFF” by sliding the button next to it.
- Click on the “Save” button to commit your changes.
That’s all! We have disabled the access requests for your site. This means that users will not be able to request access to resources if they do not have permission. Similarly, on classic SharePoint sites (and modern sites too!), follow these steps to turn off access requests.
- Click on Settings gear >> Site Permissions >> Access Requests Settings.
- This takes you to the SharePoint Online access request settings page of the site. Uncheck the “Allow access requests” checkbox and click OK to save your changes.
This disables the access requests for the specific site. However, access request settings are scoped at each site where users have unique permissions. So, If your subsite uses unique permissions, then you must repeat the above step for each subsite in your site collection (or each subsite in the tenant!). Let’s automate these steps to disable access requests for the site collection and tenant levels.
SharePoint Online: Disable Access Request using PowerShell
Using PnP PowerShell, let’s disable access requests for a given SharePoint Online site collection.
#Parameter
$SiteURL = "https://crescent.sharepoint.com/sites/Marketing"
#Function to disable access request on SharePoint Online Web
Function Disable-PnPAccessRequest
{
[cmdletbinding()]
Param(
[parameter(Mandatory = $true, ValueFromPipeline = $True)] $Web
)
Try {
Write-host -f Yellow "Disabling Access Request on:"$web.Url
If($Web.HasUniqueRoleAssignments)
{
#Disable Access Request
$Web.RequestAccessEmail = [string]::Empty
$Web.SetUseAccessRequestDefaultAndUpdate($False)
$Web.Update()
Invoke-PnPQuery
Write-host -f Green "`tAccess Request has been Disabled!"$web.Url
}
else
{
Write-host -f Yellow "`tWeb inherits permissions from the parent!"$web.Url
}
}
Catch {
write-host "`tError Disabling Access Request: $($_.Exception.Message)" -foregroundcolor Red
}
}
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
#Call the Function for all webs
Get-PnPSubWeb -IncludeRootWeb -Recurse -Includes HasUniqueRoleAssignments | ForEach-Object { Disable-PnPAccessRequest $_ }
How about disabling access requests for all sites in the tenant?
PowerShell to Disable Access Request for All Sites in the Tenant
Let me show you how to automate disabling access requests in all SharePoint Online sites in the tenant. This PowerShell script iterates through each site in the tenant and turns-OFF access requests if the site uses unique permissions:
#Parameter
$TenantAdminURL = "https://Crescent-Admin.SharePoint.com"
#Function to disable access request on SharePoint Online Web
Function Disable-PnPAccessRequest
{
[cmdletbinding()]
Param(
[parameter(Mandatory = $true, ValueFromPipeline = $True)] $Web
)
Try {
Write-host -f Yellow "Disabling Access Request on:"$web.Url
If($Web.HasUniqueRoleAssignments)
{
#Disable Access Request
$Web.RequestAccessEmail = [string]::Empty
$Web.SetUseAccessRequestDefaultAndUpdate($False)
$Web.Update()
Invoke-PnPQuery
Write-host -f Green "`tAccess Request has been Disabled!"$web.Url
}
else
{
Write-host -f Yellow "`tWeb inherits permissions from the parent!"$web.Url
}
}
Catch {
write-host "`tError Disabling Access Request: $($_.Exception.Message)" -foregroundcolor Red
}
}
#Connect to Admin Center
$Cred = Get-Credential
Connect-PnPOnline -Url $TenantAdminURL -Credentials $Cred
#Get All Site collections - Exclude: Seach Center, Mysite Host, App Catalog, Content Type Hub, eDiscovery and Bot Sites
$SitesCollections = Get-PnPTenantSite | Where -Property Template -NotIn ("SRCHCEN#0", "REDIRECTSITE#0", "SPSMSITEHOST#0", "APPCATALOG#0", "POINTPUBLISHINGHUB#0", "EDISC#0", "STS#-1")
#Loop through each site collection
ForEach($Site in $SitesCollections)
{
#Connect to site collection
Connect-PnPOnline -Url $Site.Url -Credentials $Cred
#Call the Function for all webs
Get-PnPSubWeb -IncludeRootWeb -Recurse -Includes HasUniqueRoleAssignments | ForEach-Object { Disable-PnPAccessRequest $_ }
}
Make sure you have access to all sites in your tenant before running this script! Otherwise, You may get “Access denied” or “401 unauthorized” errors! Once access requests are turned off, users with no access to the site will see this page,
instead of:
Conclusion
In conclusion, disabling access requests in SharePoint Online is a simple process that can be done by a SharePoint administrator. By disabling access requests, users will no longer be able to request access to a site or document library. Instead, the administrator will need to manually grant access to users. This can be useful in situations where the organization wants to have a tighter control over who has access to certain content. Disabling access requests also eliminates the need to constantly monitor and approve access requests, which can save time and resources for the administrator.
After modern authentication, this script isn’t working for me
Sorry never mind i was planning to comment under something else but opened wrong page. This works like a charm
SetUseAccessRequestDefaultAndUpdate is not working for me. I am looking to disabled access request from users, so that no user should be able to request access from owner. In my csom code runs and does not give any error but when i go back to the site UI, access request setting is still enabled. We are sharepoint online environment. Did this code work for you?
Do you get the “Access Request has been Disabled! URL” message? If a site inherits permissions from its parent, You have to set these settings at the parent site.
This is super helpful for disabling for existing sites. Do you know if there is a way or a setting to disable for future sites that are created in a site collection? I would like to prevent this feature in all new sites created in a specific site collection, but when I templated a site with the feature disabled, it didn’t stick within the template.