Introduction
If you need to update all document libraries in all sites in a SharePoint Online hub from using a local content type to use a content type from the content type hub, this blog post shows how you can achieve this.
Manually managing content types across multiple sites in SharePoint Online can be a daunting task, especially when dealing with a large number of document libraries within a hub site. However, by leveraging PowerShell and PnP PowerShell commands, you can automate this process. This blog post shows a script that automates the updating of content types across all document libraries in all sites connected to a SharePoint Online hub.
Script Overview
This PowerShell script performs the following tasks:
- Connects to the SharePoint Online Admin Center and the hub site.
- Retrieves all sites connected to the hub site.
- Adds a new content type from the Content Type Hub to each site.
- Iterates through all document libraries in each site:
- Checks for the existing content type.
- Adds the new content type if it does not exist.
- Sets the new content type as the default.
- Hides the old content type from the new button.
- Disables the “Allow management of content types” setting.
Prerequisites
Before running the script, ensure you have the following:
- PnP PowerShell Module: Install the latest version of the PnP PowerShell module (PnP.PowerShell).
- Admin Permissions: Make sure you have SharePoint Admin permissions to execute the script.
Script Details
Here’s a the PowerShell script:
##############################################################################################
# This script updates the content type of all document libraries in all sites in a hub
# The content type is synced from the content type hub
##############################################################################################
# Input parameters
param(
[string]$oldCTName = "[old content type name]", # The name of the content type to be replaced
[string]$newCTName = "[new content type name]", # The name of the new content type to be added
[string]$adminSiteUrl = "[admin site url]", # URL of the SharePoint Online Admin Center
[string]$hubSiteUrl = "[hub site url]" # URL of the SharePoint hub site
)
# create a log type with the name of the script and a timestamp
$LogType = "sync-cth-to-sites-in-hub"
$LogTime = Get-Date -Format "yyyy-MM-dd-HH-mm-ss"
$LogName = "$LogType-$LogTime.log"
# create a log file
$LogPath = ".\$LogName"
New-Item -Path $LogPath -ItemType File -Force | Out-Null
# Connect to the SharePoint Online Admin Center
Connect-PnPOnline -Url $adminSiteUrl -Interactive
# Get all sites connected to the hub site
$connectedSites = Get-PnPHubSiteChild -Identity $hubSiteUrl
# For each connected site
foreach ($siteUrl in $connectedSites) {
# Connect to the SharePoint Online site
Connect-PnPOnline -Url $siteUrl -Interactive
# Log the site being processed
Add-Content -Path $LogPath -Value "INFO: Processing site: $($siteUrl)"
# Add the content type from the content type hub
Add-PnPContentTypesFromContentTypeHub -ContentTypes "0x01010078E9E655DFC737409BE27204C3FF8637"
Write-Host "Added content type to $($siteUrl)"
Add-Content -Path $LogPath -Value "Added content type to $($siteUrl)"
# Get all document libraries in the site
$docLibraries = Get-PnPList | Where-Object { $_.BaseTemplate -eq 101 }
# Foreach document library add the synced content type
foreach ($docLibrary in $docLibraries) {
# Log number of documents in the library
$docCount = (Get-PnPListItem -List $docLibrary).Count
# Log the document library being processed
Write-Host "Processing document library: $($docLibrary.Title) with $docCount documents"
Add-Content -Path $LogPath -Value "INFO: Processing document library: $($docLibrary.Title) with $docCount documents"
try {
# Check if the list has a content type with the specified name
$contentTypeOriginal = Get-PnPContentType -List $docLibrary -Identity $oldCTName -ErrorAction SilentlyContinue
if ($null -eq $contentTypeOriginal) {
Write-Host "INFO: Content type DocCenter Basdokument not found in $($docLibrary.Title)"
continue
}
# Check if the list has a content type with the specified name
$contentTypeNew = Get-PnPContentType -List $docLibrary -Identity $newCTName -ErrorAction SilentlyContinue
if ($null -ne $contentTypeNew) {
Write-Host "INFO: Content type ($newCTName) already exists in $($docLibrary.Title)"
Add-Content -Path $LogPath -Value "INFO: Content type ($newCTName) already exists in $($docLibrary.Title)"
continue
}
# Add the content type to the document library
Add-PnPContentTypeToList -List $docLibrary -ContentType $newCTName
Add-Content -Path $LogPath -Value "INFO: Added content type $($newCTName) to $($docLibrary.Title)"
# Set the content type as the default content type
try {
Set-PnPDefaultContentTypeToList -List $docLibrary -ContentType $newCTName
Add-Content -Path $LogPath -Value "INFO: Set content type $($newCTName) as default for $($docLibrary.Title)"
}
catch { Add-Content -Path $LogPath -Value "ERROR: Failed to set content type $($newCTName) as default for $($docLibrary.Title): $_" }
# Change the setting "allow management of content types" to false
try {
Set-PnPList -Identity $docLibrary -EnableContentTypes $false
Add-Content -Path $LogPath -Value "INFO: Disabled management of content types in $($docLibrary.Title)"
}
catch { Add-Content -Path $LogPath -Value "ERROR: Failed to disable management of content types in $($docLibrary.Title): $_" }
}
catch {
Write-Error "Failed to process document library $($docLibrary.Title): $_"
}
finally {
}
}
}
Key Functions in the Script
- Connecting to SharePoint Online: The script uses
Connect-PnPOnlineto connect to the SharePoint Online Admin Center and each individual site. The-Interactiveparameter prompts for credentials. - Retrieving Sites Connected to the Hub: The
Get-PnPHubSiteChildcommand retrieves all sites connected to the specified hub site. - Adding Content Types from the Content Type Hub: The
Add-PnPContentTypesFromContentTypeHubcommand makes sure that the new content type from the Content Type Hub is available to use in the libraries. - Processing Document Libraries: The script iterates over each document library in each site, checks if the existing content type is used in the library, adds the new content type, and sets the appropriate default settings.
Log Management
The script generates a log file with details of each operation, including information on processed sites, document libraries, and any errors encountered. This log file can be useful for auditing purposes or troubleshooting issues after execution.
Running the Script
To execute the script:
- Open PowerShell in Administrator mode.
- Run the script with the necessary parameters
.\Update-ContentType.ps1 -oldCTName "OldContentType" -newCTName "NewContentType" -adminSiteUrl "https://yourtenant-admin.sharepoint.com" -hubSiteUrl "https://yourtenant.sharepoint.com/sites/YourHubSite"
Disclaimer
This script is provided as-is, without any guarantees or warranties. Use it at your own risk. Please be aware that SharePoint Online environments can vary significantly based on custom configurations, policies, and organizational requirements. It is highly recommended to thoroughly review and test the script in a controlled environment before deploying it in production. Make any necessary modifications to suit your specific setup and always ensure you have appropriate backups and permissions before running any scripts that modify your SharePoint environment. The author is not responsible for any issues or data loss that may occur from the use of this script.