Skip to content

Instantly share code, notes, and snippets.

@bic742
Last active July 14, 2023 16:51
Show Gist options
  • Save bic742/66da9374e972f570e6a12938006d336f to your computer and use it in GitHub Desktop.
Save bic742/66da9374e972f570e6a12938006d336f to your computer and use it in GitHub Desktop.
$datasource = Get-Item .
$referrers = $datasource | Get-ItemReferrer
$pageOnlyReferrers = $referrers | Where-Object { $_.Name -ne '__Standard Values' } | Sort-Object -Property Name -Unique
$pageOnlyReferrers = $pageOnlyReferrers | Where-Object { $_.Paths.Path -like '/sitecore/content/{site-node}' }
if ($pageOnlyReferrers.Count -eq 0) {
return;
}
$javascript = @"
var list = document.getElementById("datasource-list");
if (window.getComputedStyle(list).display === "block") {
list.style.display = "none";
}
else {
list.style.display = "block";
}
"@
$text = @"
<fieldset>
<legend onclick='$javascript'>Click to view pages for this datasource</legend>
<ul style='display: none;' id='datasource-list'>
"@
foreach ($referrer in $pageOnlyReferrers) {
$linkAction = "scForm.postRequest('', '', '', 'item:load(id=$($referrer.Id))');"
$text = "$text<li>$($referrer.Name): <a href='#' onclick='$linkAction'>$($referrer.Paths.Path)</a></li>"
}
$text = "$text</ul></fieldset>"
$warning = $pipelineArgs.Add()
$warning.Text = $text
$warning.HideFields = $false
$item = Get-Item .
$renderings = $item | Get-Rendering -FinalLayout
$javascript = @"
var list = document.getElementById("datasource-list");
if (window.getComputedStyle(list).display === "block") {
list.style.display = "none";
}
else {
list.style.display = "block";
}
"@
$text = @"
<fieldset>
<legend onclick='$javascript'>Click to view datasources on this page</legend>
<ul style="display: none;" id='datasource-list'>
"@
foreach ($rendering in $renderings) {
if ([string]::IsNullOrEmpty($rendering.Datasource)) {
continue
}
$renderingItem = Get-Item -Path "master:$($rendering.ItemId)"
$datasource = Get-Item -Path "master:$($rendering.Datasource)"
if ($datasource) {
$datasourceId = $datasource.Id.ToString().Replace('{', '').Replace('}', '')
$listItem = "<li>$($renderingItem.Name): <a href='/sitecore/shell/Applications/Content%20Editor?id=$datasourceId&fo=$datasourceId&sc_content=master'>$($datasource.Paths.Path)</a></li>"
$text = "$text$listItem"
}
}
$text = "$text</ul></fieldset>"
$warning = $pipelineArgs.Add()
$warning.Text = $text
$warning.HideFields = $false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment