|
| 1 | +--- |
| 2 | +plugin: add-to-gallery |
| 3 | +--- |
| 4 | + |
| 5 | +# Update Content type from Hub on sites |
| 6 | + |
| 7 | +## Summary |
| 8 | + |
| 9 | +Back in the days Microsoft would update the content types in the content type hub and then push it to all site collections. This is no longer the case. You need to run a script to update the content types in all site collections. |
| 10 | +This is such a script. It will check all site collections and update the content type if it exists. |
| 11 | + |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | + |
| 16 | +# [PnP PowerShell](#tab/pnpps) |
| 17 | + |
| 18 | +```powershell |
| 19 | +
|
| 20 | +#when a content type in the Content type gallery is updated and republished |
| 21 | +# it is not automaticly updated on the site collections where it is used. |
| 22 | +
|
| 23 | +# This script will check every site collection and update the content type |
| 24 | +
|
| 25 | +$adminUrl = "https://contoso-admin.sharepoint.com/" |
| 26 | +$pnpClientId = "the client id of the PnP Rocks app" |
| 27 | +$contenttypeName = "Contoso Project Document" |
| 28 | +
|
| 29 | +if(-not $conn) |
| 30 | +{ |
| 31 | + $conn = Connect-PnPOnline -Url $adminUrl -Interactive -ClientId $pnpClientId -ReturnConnection -WarningAction Ignore |
| 32 | +} |
| 33 | +
|
| 34 | +$allsites = Get-PnPTenantSite -Connection $conn -ErrorAction Stop |
| 35 | +foreach($site in $allsites) |
| 36 | +{ |
| 37 | + Write-Host "Site: $($site.Url)" |
| 38 | + try |
| 39 | + { |
| 40 | + $siteUrl = $site.Url |
| 41 | + Connect-PnPOnline -Url $siteUrl -Interactive -ClientId $pnpClientId -WarningAction Ignore |
| 42 | + $contenttype = Get-PnPContentType -ErrorAction SilentlyContinue -Identity $contenttypeName |
| 43 | + if($contenttype) |
| 44 | + { |
| 45 | + Write-Host "Updating content type: $($ct.Name) at site: $siteUrl " -ForegroundColor Green |
| 46 | + Add-PnPContentTypesFromContentTypeHub -ContentTypes $ct.Id -ErrorAction Stop |
| 47 | + } |
| 48 | + else |
| 49 | + { |
| 50 | + Write-Host "No content type: $($ct.Name) at site: $siteUrl " |
| 51 | + } |
| 52 | + } |
| 53 | + catch |
| 54 | + { |
| 55 | + <#Do this if a terminating exception happens#> |
| 56 | + throw $_ |
| 57 | + } |
| 58 | +} |
| 59 | +
|
| 60 | +
|
| 61 | +``` |
| 62 | +[!INCLUDE [More about PnP PowerShell](../../docfx/includes/MORE-PNPPS.md)] |
| 63 | +*** |
| 64 | + |
| 65 | + |
| 66 | +## Contributors |
| 67 | + |
| 68 | +| Author(s) | |
| 69 | +|-----------| |
| 70 | +| Kasper Larsen | |
| 71 | + |
| 72 | +[!INCLUDE [DISCLAIMER](../../docfx/includes/DISCLAIMER.md)] |
| 73 | +<img src="https://m365-visitor-stats.azurewebsites.net/script-samples/scripts/spo-update-contentype-from-hub" aria-hidden="true" /> |
0 commit comments