SharePoint Online–CSOM Change Access Requests Settings

The Access Request in the Site settings > Site permissions is a feature which allows people to request access to content that they do not currently have permission to see. The setting that allows members to share the site and individual files and folders doesn’t overwrite the "Manage permissions" permission level, it works with permission level.  When you select the "Allow members to share the site and individual files and folders" setting, the site members can send an invitation to the other users who have no permission, but this invitation will not take effect until the site owner approve it. As a site owner, you can go to Site settings > Access requests and invitations to approve or reject the invitation.

Default setting:

image

 

You can change these settings programmatically using CSOM. 

To uncheck Allow Members to share the site and invite others, use this code;        

             // web is Microsoft.SharePoint.Client.Web object

            web.Context.Load(web, w => w.MembersCanShare);                web.Context.ExecuteQuery();             web.MembersCanShare = false;             web.Update();             web.Context.Load(web, w => w.MembersCanShare);             web.Context.ExecuteQuery();

            web.Context.Load(web, w => w.AssociatedMemberGroup.AllowMembersEditMembership);             web.Context.ExecuteQuery();             web.AssociatedMemberGroup.AllowMembersEditMembership = false;             web.AssociatedMemberGroup.Update();             web.Context.Load(web, w => w.AssociatedMemberGroup.AllowMembersEditMembership);             web.Context.ExecuteQuery();

To set a custom email address for Allow Access Requests, for e.g. email address of Site Owner, use this code;  RequestAccessEmail property is available from Version 16.1.4727.1200 onwards .   You can find the latest CSOM package for SharePoint online from the NuGet gallery with an id of Microsoft.SharePointOnline.CSOM

          clientContext.Load(clientContext.Web, w => w.RequestAccessEmail);           clientContext.ExecuteQuery();           clientContext.Web.RequestAccessEmail = clientContext.Site.Owner.Email;           clientContext.Web.Update();           clientContext.Load(clientContext.Web, w => w.RequestAccessEmail);           clientContext.ExecuteQuery();

 

Custom setting using CSOM:

image