Instantly share code, notes, and snippets.

Embed
What would you like to do?
A Sitecore PowerShell Extensions script for clearing and applying profiles to one or more items
$rootItem = Get-Item .
if ($rootItem.Appearance.ReadOnly) {
Show-Alert "You cannot edit this item because it is protected."
return
}
if (!$rootItem.Security.CanWrite([Sitecore.Context]::User)) {
Show-Alert "You do not have write access to this item in order to perform this action."
return
}
$settings = @{
Title = "Reset Profiles"
Description = "Clear and assign profiles for one or more items."
Width = "800"
Height = "500"
OkButtonName = "Proceed"
CancelButtonName = "Abort"
Parameters =
@{ Name = "root"; Title = "Root Item"; Value = $rootItem.Paths.ContentPath; Editor = "info" },
@{ Name = "descendants"; Title = "<span style=""top: -4px;position: relative;"">Include all descendants of this item</span>"; Value = $false },
@{ Name = "presets"; Title = "Assign new ""Profile Cards""<br /><span style=""font-weight:normal; font-style:italic; display: block;"">(Multiple Profile Cards under the same profile will be distributed equally)</span><br />"; Source = "DataSource=/sitecore/system/Marketing Control Panel/Profiles&DatabaseName=master&IncludeTemplatesForSelection=Profile Card"; Editor = "treelist" }
}
$result = Read-Variable @settings
if ($result -ne "ok") {
return
}
if ($presets) {
$confirmPresets = [string]::Join("<br />", $presets.Name)
}
else {
$confirmPresets = "<span style=""color: red"">None (existing profiles will be cleared)</span>"
}
$rootItemPath = $rootItem.Paths.ContentPath
$descendantsWarning = if ($descendants) { " <b>and ALL descendant items</b>" } else { "" }
$intro = "<span style=""color: red"">Pre-existing profiles on the root item ($rootItemPath)$descendantsWarning will be removed and replaced with the Profile Cards defined below.</span>"
$settings = @{
Title = "Warning"
Description = "Please ensure that the information below is correct before proceeding."
OkButtonName = "Everything looks good, run it!"
CancelButtonName = "Abort"
Parameters =
@{ Name = "warning"; Title = ""; Value = $intro; Editor = "info" },
@{ Name = "root"; Title = "Root Item"; Value = $rootItem.Paths.ContentPath; Editor = "info" },
@{ Name = "descendants"; Title = "Include all descendants of this item"; Value = $descendants.ToString(); Editor = "info" },
@{ Name = "presets"; Title = "Assign new ""Profile Cards""<br /><span style=""font-weight:normal; font-style:italic; display: block;"">(Multiple Profile Cards under the same profile will be distributed equally)</span>"; Value = $confirmPresets; Editor = "info" }
}
$confirm = Read-Variable @settings
if ($confirm -ne "ok") {
return
}
$items = New-Object System.Collections.Generic.List[System.Object]
function Get-ItemsRecursive($item) {
$items.Add($item)
if ($descendants) {
foreach ($child in $item.Children) {
Get-ItemsRecursive $child
}
}
}
Get-ItemsRecursive $rootItem
foreach ($item in $items) {
if (!$item.Appearance.ReadOnly -and $item.Security.CanWrite([Sitecore.Context]::User) -and $item.Visualization.Layout) {
[Sitecore.Analytics.Data.TrackingField] $trackingField = $item.Fields["__tracking"]
foreach ($contentProfile in $trackingField.Profiles) {
$matches = $presets | Where-Object { [Sitecore.Analytics.Data.ProfileUtil]::GetProfileItem($_).ID -eq $contentProfile.ProfileID }
if ($matches) {
# Clear out existing presets
if ($contentProfile.Presets) {
$contentProfile.Presets.Clear()
}
else {
$contentProfile.Presets = New-Object "System.Collections.Generic.Dictionary``2[System.String,float]"
}
# Assign new presets with equal weight
$percentage = [math]::Round(100 / $matches.Count, 5)
foreach ($match in $matches) {
$contentProfile.Presets.Add($match.Key, $percentage)
}
# Save value
$contentProfile.SaveToField = $true
}
else {
# Remove profile/presets/keys
$contentProfile.SaveToField = $false
}
}
# Recalculate keys
$updatedTrackingField = New-Object Sitecore.Analytics.Data.TrackingField -ArgumentList $trackingField.InnerField, $trackingField.GetFieldValue()
# Update field
$item.Editing.BeginEdit()
$item.Fields["__tracking"].Value = $updatedTrackingField.GetFieldValue()
$item.Editing.EndEdit() | Out-Null
Write-Host "Updated Item:" $item.Paths.ContentPath
}
else {
Write-Host "Skipped Item:" $item.Paths.ContentPath
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment