Applying a Logo to Every Site in SharePoint Online

A quick post today on a fairly common problem - you're given a site collection on Office 365 that has grown organically with hundreds of nested sites, four or five levels deep in places. The various site owners have used many different versions of your company logo, and you want to reintroduce a degree of consistency by applying the same logo to every site in a site collection.

The most efficient way to do this is to run some client-side code from PowerShell. Basically you need to iterate over the subsites of a given site and set the Web.SiteLogoUrl property on every site. The easiest way to do this is to:
  1. Install the excellent SharePointPnP.PowerShell cmdlets.
  2. Run a script something like the one below.
# UpdateSiteLogos.ps1
# Jason Lee 5/12/16

# Variables
$rootSiteUrl = "https://[tenant].sharepoint.com/sites/[path]"
$credentialManagerLabel = "adminface"
$logoUrl = "/sites/[path]/SiteAssets/smile.png"

# Recursive function to set site logo on specified site
# Provide the server-relative URL of the root site to start
function updateSiteLogo {
    Param(
        [parameter(Mandatory=$true)]       
        [String]
        $serverRelativeUrl
    )
    $web = Get-PnPWeb -Identity $serverRelativeUrl
    Write-Output ("Updating site {0}" -f $web.ServerRelativeUrl)
    $web.SiteLogoUrl = $logoUrl
    $web.Update()
    Execute-PnPQuery
   
    $subwebs = Get-PnPSubWebs -Web $web
    foreach ($subweb in $subwebs) {       
        updateSiteLogo($subweb.ServerRelativeUrl)
    }
   
}

# Connect to the site collection, then call updateSiteLogo on the root web
Connect-PnPOnline -Url $rootSiteUrl -Credentials $credentialManagerLabel
$rootweb = Get-PnPWeb
Write-Output "Setting site logos..."
updateSiteLogo($rootweb.ServerRelativeUrl)
Write-Output "Done."

The Get-PnPWeb cmdlet actually has its own Recurse option, and you could use that if you want to rather than doing your own recursion. I steered away from it because it gets all the subsites in one hit (the right approach in most circumstances), which can cause the script to hang for a while if you've got hundreds of nested sites. This approach instead walks the site structure one level at a time.


Comments

Popular posts from this blog

Server-side activities have been updated

The target principal name is incorrect. Cannot generate SSPI context.

Custom Workflow Activity for Creating a SharePoint Site