Useful scripts | Basic and Advanced Day to day used scripts

the scripts which are found rarely on the internet but needed often

Office 365 – Programmatically disable non-owners sharing

leave a comment »


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

Written by ubk

August 30, 2016 at 3:40 pm

Posted in Scripts

Follow

Get every new post delivered to your Inbox.

Join 55 other followers