2

I'd like to apply a common alternate CSS URL for all my subsites. If I go through UI, I can do this by going to Site Settings > Master Page > Set the URL in the text box and make sure that the checkbox to have the subsites get reset with it is checked as well.

Through PowerShell CSOM code though, what is the equivalent of this checkbox? This is what I currently have but it only works for the current site:

  $web = $Context.web
  $Context.Load($web)
  $Context.ExecuteQuery()

  $alternateCssUrl = 
  "<my CSS url>"

  $web.AlternateCssUrl = $alternateCssUrl
  $web.AllProperties["__InheritsAlternateCssUrl"] = $true
  $web.Update()
  $Context.ExecuteQuery()
2

You just need to get all subsites and loop through all sites and update the property.

Your code can be like below

$sSiteColUrl = "<<site collection URL>>"
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($sSiteColUrl)    
$Context.Credentials = $spoCredentials 

$rootWeb = $Context.Web  
$webs  = $rootWeb.Webs 

$Context.Load($webs) 
$Context.ExecuteQuery() 

foreach($web in $webs) 
{     
    $web = $Context.web
    $Context.Load($web)
    $Context.ExecuteQuery()

    $alternateCssUrl = "<my CSS url>"

    $web.AlternateCssUrl = $alternateCssUrl
    $web.AllProperties["__InheritsAlternateCssUrl"] = $true
    $web.Update()
    $Context.ExecuteQuery()
}

EDIT - Use following script to update the recursive site. This will update all subsites of a subsite also.

function Get-SPOSubWebs{ 
    Param( 
    [Microsoft.SharePoint.Client.ClientContext]$Context, 
    [Microsoft.SharePoint.Client.Web]$RootWeb 
    )

    $Webs = $RootWeb.Webs 
    $Context.Load($Webs) 
    $Context.ExecuteQuery() 

    ForEach ($Web in $Webs) 
    {   

        $Context.Load($Web) 
        $Context.ExecuteQuery()

        Write-Host $Web.Title

        $alternateCssUrl = "<my CSS url>"

        $Web.AlternateCssUrl = $alternateCssUrl
        $Web.AllProperties["__InheritsAlternateCssUrl"] = $true
        $Web.Update()
        $Context.ExecuteQuery()

        Get-SPOSubWebs -RootWeb $Web -Context $Context 
    } 
} 

Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll" | Out-Null 
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll" | Out-Null 


$securePassword = Read-Host -Prompt "Enter your password: " -AsSecureString
$UserName = "<<user name>>"
$SiteUrl = "<<site collection url>>"
$spoCred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $securePassword) 
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)  
$ctx.Credentials = $spoCred 

$Web = $ctx.Web 

Get-SPOSubWebs -Context $ctx -RootWeb $Web  
  • Does it go through all the subsites including the ones underneath other subsites? Or is this just for the ones directly under the rootweb? – Katie Jun 14 '17 at 21:23
  • This will update the only first level of subsites. – Venkat Konjeti Jun 14 '17 at 21:26
  • If you need help with recursive webs using PowerShell. Go through the example in this link gallery.technet.microsoft.com/office/… – Venkat Konjeti Jun 14 '17 at 21:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.