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

Advertisements

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: https://www.microsoft.com/en-us/download/details.aspx?id=35585)
2. SharePoint Online Management Shell (URL: https://www.microsoft.com/en-in/download/details.aspx?id=35588)

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

#Specify tenant admin and site URL
$User = "yourusername@yoursite.onmicrosoft.com"
$SiteURL = "https://yoursite.sharepoint.com/sites/demosite"

#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())

$Context.Load($List)
$Context.Load($ListItems)
$Context.ExecuteQuery()

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"

Cheers!!

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:

https://blogs.technet.microsoft.com/fromthefield/2014/02/18/office-365-powershell-script-to-create-a-list-add-fields-and-change-the-default-view-all-using-csom/