Last active
November 10, 2019 02:07
-
-
Save fluxdigital/c551c79f939702dcb7988a7de23c8a06 to your computer and use it in GitHub Desktop.
Updates a field value to the same value as the standard value but updates the field so it is no longer using the standard value
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$templateIds = | |
'{565F2D63-1359-726C-AB77-F3DA34C90AD0}', | |
'{3465B346-3B37-8E40-AB70-31E17F4F38H4}', | |
'{78309E9F-49A6-91C9-9DC3-73F340CE6690}' | |
$global:usingStandardValues = 0 | |
$global:notUsingStandardValues = 0 | |
$global:updateContentTypeField = $false | |
$global:fieldName = "content type" | |
Write-Host "---Starting Update Content Types Script---" -ForegroundColor DarkCyan | |
Write-Host "Update Content Type Field Setting = $($global:updateContentTypeField)" | |
Write-Host "Getting Pages to update..." | |
$pages = Get-ChildItem -Path "/sitecore/content/Site1/Home" -Recurse | Where-Object {$templateIds -contains $_.TemplateID} | |
foreach($page in $pages){ | |
$contentTypeId = $page["$($global:fieldName)"]; | |
$contentType = Get-Item -Path "master:/$($contentTypeId)"; | |
#check if field is using the standard values or not | |
$contentTypeField = Get-ItemField -Item $page -ReturnType Field -Name "content type" | |
if ($contentTypeField.ContainsStandardValue){ | |
$global:usingStandardValues +=1; | |
Write-Host "$($page.Name) is using standard value - $($contentType.Name) ($($contentTypeId))." -ForegroundColor Yellow | |
if($global:updateContentTypeField){ | |
New-UsingBlock (New-Object Sitecore.Data.BulkUpdateContext) { | |
#temp update to temp item value to force "ContainsStandardValue" change | |
$page.Editing.BeginEdit() | |
$page["$($global:fieldName)"] = "{1F342E82-HF89-876D-92AF-63F05CBF2636}" #temp content type item | |
$page.Editing.EndEdit() | Out-Null | |
Write-Host "Content Type Updated To Temp Content Type Item: 1F342E82-HF89-876D-92AF-63F05CBF2636 - $($page.Paths.FullPath)" -ForegroundColor DarkGreen | |
#now set it back to same content type value again | |
$page.Editing.BeginEdit() | |
$page["$($global:fieldName)"] = $contentTypeId | |
$page.Editing.EndEdit() | Out-Null | |
Write-Host "Content Type Updated To Content Type Item: $($contentType.Name) - $($contentTypeId)" -ForegroundColor Green | |
} | |
} | |
} | |
else{ | |
$global:notusingStandardValues +=1; | |
Write-Host "$($page.Name) - is NOT using standard value - Skipping Page Item." -ForegroundColor Magenta | |
} | |
} | |
Write-Host "---Script Completed---" -ForegroundColor DarkCyan | |
Write-Host "-----------------------------------------" | |
Write-Host "Pages Using standard values for Content Type field: $($global:usingStandardValues)" | |
Write-Host "Pages Not using standard values for Content Type field: $($global:notUsingStandardValues)" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment