Office 365 – Programmatically disable non-owners sharing


Working with CSOM or PowerShell to manage Site Settings, this particular option came to me as a small challenge and following I found to be the solution towards it.

2016-08-30 20_56_59-Permissions_1

If you are using SharePoint Online cmdlets, then following is the proper way. Please note, this will also disable the access requests.

$adminUPN = "user@yoursite.onmicrosoft.com"
$adminPassword = "yourpassword"
#SharePoint Online Site URL where you want to change
$SiteURL = "https://yoursite.sharepoint.com/sites/testsite"
#SharePoint Online Tenant Administration URL
$AdminSiteURL = "https://yoursite-admin.sharepoint.com"

$AdminUserCredential = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $adminUPN, $(convertto-securestring $adminPassword -asplaintext -force)

#Connect to to Office 365
try{
    Connect-SPOService -Url $AdminSiteURL -Credential $AdminUserCredential
    Write-Host "Info: Connected successfully to Office 365"

    try{
        Write-Host "Info: Updating 'Access Request Settings'"
        Set-SPOSite -Identity $SiteURL -DisableSharingForNonOwners
    }
    catch{
        Write-Host "Exception: Issues while Updating 'Access Request Settings'. $($error[0].Exception.Message)"
    }
}
catch{
    Write-Host "Exception: Could not connect to Office 365. $($error[0].Exception.Message)"
    exit
}

 

If you want to use .NET Managed CSOM then you can use the following method

 

 // web is Microsoft.SharePoint.Client.Web object
 // Disable Sharing Option for Memebers
            clientContext.Load(web, w => w.MembersCanShare);
            clientContext.ExecuteQuery();
            web.MembersCanShare = false;
            web.Update();
            clientContext.Load(web, w => w.MembersCanShare);
            clientContext.ExecuteQuery();

// Disable 'Allow members to invite others to the site members group' settings
            clientContext.Load(web, w => w.AssociatedMemberGroup.AllowMembersEditMembership);
            clientContext.ExecuteQuery();
            web.AssociatedMemberGroup.AllowMembersEditMembership = false;
            web.AssociatedMemberGroup.Update();
            clientContext.Load(web, w => w.AssociatedMemberGroup.AllowMembersEditMembership);
            clientContext.ExecuteQuery();

/* To set a Request Access email. To use this you need latest CSOM assemblies  Version 16.1.4727.1200 */
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();

Following should be the output from your script:

2016-08-30 20_57_42-Permissions_ 2

4 thoughts on “Office 365 – Programmatically disable non-owners sharing

  1. Hi There,

    i tried the same script , but “Allow access requests” is not unchecked. any idea? but the first two check boxes are unchecked. not sure why the 3rd check box is not unchecked.. please help.

    • Hi Manoj,

      Sorry for replying late. I did check the script as-is right now and I saw that the ‘Allow access requests’ is getting unchecked properly. Please use an account with appropriate access.

      Access request settings updated properly.

  2. >If you are using SharePoint Online cmdlets, then following is the proper way. Please note, this will also disable the access requests.

    This method does not disable access requests. Do you have a method to disable access requests?

    • I have just tried the script as-is and I can confirm that it removed the checkbox for ‘Allow access request’ settings in my SharePoint online site properly. May be you are using an account without appropriate access.

      Access request settings updated properly

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s