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 = ""
$adminPassword = "yourpassword"
#SharePoint Online Site URL where you want to change
$SiteURL = ""
#SharePoint Online Tenant Administration URL
$AdminSiteURL = ""

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

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

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


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);
            web.MembersCanShare = false;
            clientContext.Load(web, w => w.MembersCanShare);

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

/* 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.Web.RequestAccessEmail = clientContext.Site.Owner.Email;
          clientContext.Load(clientContext.Web, w => w.RequestAccessEmail);

Following should be the output from your script:

2016-08-30 20_57_42-Permissions_ 2

Office 365 – SharePoint Powershell – Read List

We have moved to SharePoint online which is under Office 365 platform. I will share some basic examples, with which I have started the journey to this new platform myself.

First, make sure you have downloaded the following items in your local machine:

1. SharePoint Client Components SDK (URL:
2. SharePoint Online Management Shell (URL:

Now, let us create a small script to connect to our Office 365 tenant using PowerShell:

#Specify tenant admin and site URL
$User = ""
$SiteURL = ""

#Add references to SharePoint client assemblies and authenticate to Office 365 site – required for CSOM
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

$Password = Read-Host -Prompt "Please enter your password" -AsSecureString
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$Password)

#Bind to site collection
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$Password)
$Context.Credentials = $Creds

Ok, now, you have connected PowerShell to Office 365 SharePoint site. Let’s
see how to connect to a list and fetch the items from it.

$ListTitle = "Your List Title"

#Retrieve list
$List = $Context.Web.Lists.GetByTitle($ListTitle)

#Get All items from the list
$ListItems = $List.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery())


Yes, all your items are now available in $ListItems collection. All you have to do now is traverse through it and do whatever you like. Following code is given to display few columns from it.

#Showlist items count
Write-Host $List.ItemCount
Write-Host $ListItems.Count

#Loop through items
foreach($Item in $ListItems){
    $msg = [string]::Format("Title {0}, Company {1}, WorkCity {2}", $Item["Title"], $Item["Company"], $Item["WorkCity"])
    Write-Host $msg

Write-Host "End of Script"


Note: If you want to see how to create a list, add fields to it from Site columns and update the default view, then you can check this beautiful article which has helped me to get started: