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 onwardsYou 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


Comments (3)

  1. Soren Pedersen says:

    Will this CSOM code work on the Site Collection level only, or can it be used for sub sites as well?
    If you change these settings on the Site Collection level, then new sub sites doesn’t inherit. They start with default, where both ‘Allow members to share…’ and ‘Allow members to invite others…’ are selected.

  2. Amit says:

    There is no object “MembersCanShare” in web of client context. Could you please specify it

  3. Dipan Gandhi says:

    Hi,

    Thanks for the information, I am using powershell so to uncheck both the checkboxes, I used following code:
    $Ctx.Load($Ctx.web);
    $Ctx.Load($Ctx.web.AllProperties);
    $Ctx.web.RequestAccessEmail = “dipan.gandhi@gmail.com”;
    $Ctx.web.MembersCanShare = $False;
    $Ctx.web.AssociatedMemberGroup.AllowMembersEditMembership = $False;
    $Ctx.web.Update();
    $Ctx.ExecuteQuery();

    But after executing this code it unchecks first checkbox and the second one (Allow members to invite others to the site members group, ExternalSharing-7 Members. This setting must be enabled to let members share the site.) remains checked. Is it some another property which needs to set to false….?