OneDrive for Business: Disable access requests on company level using C# and CSOM
Access Request Settings
In SharePoint and OneDrive for Business site administrators can allow users to ask for permission on sites they don't have access to. When the user enters a url of a site they doesn't have permissions to, they will receive the following message:
Since 2016 you can manage these settings centrally for the entire organization. Going to site settings, the site administrator, i.e. owner of the OneDrive for Business will see the following message:
ODBAccessRequests property
The ODBAccessRequests property came with New SharePoint CSOM version released for SharePoint Online - October 2016. It lets administrators set policy on access requests and requests to share in OneDrive for Business. It accepts three values:
On- Users without permission to share can trigger sharing requests to the OneDrive for Business owner when they attempt to share. Also, users without permission to a file or folder can trigger access requests to the OneDrive for Business owner when they attempt to access an item they do not have permissions to.
Off- Prevent access requests and requests to share on OneDrive for Business. On site level the user will see grayed out options:
- Unspecified- Let each OneDrive for Business owner enable or disable access requests and requests to share on their OneDrive.
C#
- First create a ClientContext and test your connection with .ExecuteQuery()
ClientContext ctx = new ClientContext(url);
ctx.Credentials = new Microsoft.SharePoint.Client.SharePointOnlineCredentials(username, password);
Tenant spoTenant= new Microsoft.Online.SharePoint.TenantAdministration.Tenant(ctx);
ctx.ExecuteQuery();
- We are changing settings on tenant level, so we need a Microsoft.Online.SharePoint.TenantAdministration.Tenant object.
ctx.Load(spoTenant);
ctx.ExecuteQuery();
spoTenant.ODBAccessRequests = Microsoft.SharePoint.Client.SharingState.Off;
ctx.Load(spoTenant);
ctx.ExecuteQuery();