As described in my previous post SharePoint Online ‘Sync’ that how you can control the sync option on SharePoint document library or SharePoint site.

Sometimes you find that ‘download the offline connectivity’ option is not visible in site setting or document library advanced setting or if you want to manage sync through PowerShell.

Below is the PnP script :

Connect-PnPOnline -Url "https://expertbrains.in/sites/sitename" -UseWebLogin
$Web = Get-PnPWeb -Includes ExcludeFromOfflineClient
$web.ExcludeFromOfflineClient = $false
$web.Update()
Invoke-PnPQuery
Write-Host "Offline Client Availability is Enabled!" -f Green

Please refer Microsoft document for PnP PowerShell overview

CSOM script:

Add-Type -Path "C:\temp\microsoft.sharepointonline.csom.16.1.6008.1200\lib\net45\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\temp\microsoft.sharepointonline.csom.16.1.6008.1200\lib\net45\Microsoft.SharePoint.Client.runtime.dll"

$siteUrl = Read-Host -Prompt "Enter Site Collection URL"
$username = Read-Host -Prompt “Enter username”
$password = Read-Host -Prompt “Enter password” -AsSecureString
$subwebcheck = Read-Host -Prompt "Do you want to process subsites? (Y/N)"

# Generate ClientContext function so we can reuse
function GetClientContext($siteurl, $username, $password) {
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteurl)
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $password)
$ctx.Credentials = $credentials
return $ctx
}

$ctx = GetClientContext $siteurl $username $password

if ($ctx.ServerObjectIsNull.Value) {
Write-Host "Unable to connect to: '$siteUrl'" -ForegroundColor Red
} else {
Write-Host "Connected to: '$($siteUrl)'" -ForegroundColor Green

$rootWeb = $ctx.Web
$ctx.Load($rootWeb)
$ctx.ExecuteQuery()

# update root site
Write-Host $rootWeb.Url "is being updated to exclude from offline clients"
$rootWeb.ExcludeFromOfflineClient=$true
$rootWeb.Update()
$ctx.Load($rootWeb)
$ctx.ExecuteQuery()
Write-Host "ExcludeFromOfflineClient is now" $rootWeb.ExcludeFromOfflineClient "for the site:" $rootWeb.Url -ForegroundColor Green

if ($subwebcheck -eq "Y") {

# work with subsites
Write-Host "Processing subsites..." -ForegroundColor Yellow
$childWebs = $rootWeb.Webs
$ctx.Load($childWebs)
$ctx.ExecuteQuery()
foreach ($childWeb in $childWebs)
{
processsubsites $childWeb.url
}
}

# function to loop through subsites and setting values
function processsubsites ($siteurl){
$ctx = GetClientContext $siteurl $username $password
$rootWeb = $ctx.Web
$childWebs = $rootWeb.Webs
$ctx.Load($rootWeb)
$ctx.Load($childWebs)
$ctx.ExecuteQuery()

# perform update
if($rootWeb.WebTemplate -ne "APP"){
Write-Host $rootWeb.Url "is being updated to exclude from offline clients"
$rootWeb.ExcludeFromOfflineClient=$true
$rootWeb.Update()
$ctx.Load($rootWeb)
$ctx.ExecuteQuery()
Write-Host "ExcludeFromOfflineClient is now" $rootWeb.ExcludeFromOfflineClient "for the site:" $rootWeb.Url -ForegroundColor Green
}

# loop subsites of subsites
foreach ($childWeb in $childWebs)
{
processsubsites $childWeb.url
}
}


}