Skip to content

Instantly share code, notes, and snippets.

@hombreDelPez
Last active May 29, 2018 09:50
Show Gist options
  • Save hombreDelPez/78e76c3a2d2d5f8a7cbb1a89d08c1830 to your computer and use it in GitHub Desktop.
Save hombreDelPez/78e76c3a2d2d5f8a7cbb1a89d08c1830 to your computer and use it in GitHub Desktop.
Sitecore PowerShell script to list all orphaned datasources
<#
.SYNOPSIS
Lists the datasources which are not or no longer referenced to a rendering in a page.
.NOTES
Ramon Bruelisauer (original)
Manuel Fischer (adjustements for the Master Solution)
#>
<##
# filter IsCandidateItem
# ----------------------
# Check whether the given item is based on a Master component template and is a child item
# of a 'Local Data' or 'Shared Data' folder.
#>
filter IsCandidateItem {
param(
[Parameter(Mandatory=$true, ValueFromPipeline=$true)]
$Item,
[Parameter(Mandatory=$true)]
$Templates
)
Process {
$isCandidateTemplate = $Templates.Contains($Item.TemplateName)
If ($isCandidateTemplate) {
$parentItem = $Item.Parent
$isLocalData = $parentItem.TemplateName -eq "Local Data"
$isSharedData = $parentItem.TemplateName -eq "Shared Data"
If ($isLocalData -or $isSharedData) {
$Item
}
}
}
}
<##
# filter HasNoReferrers
# ---------------------
# Check whether the given item is not linked to any field, item or rendering.
#>
filter HasNoReferrers {
param(
[Parameter(Mandatory=$true, ValueFromPipeline=$true)]
$Item
)
Process {
$referrers = Get-ItemReferrer -Item $Item
If ($referrers -eq $null -or $referrers.Count -eq 0) {
$Item
}
}
}
##
# Get the content root item from master database
#
$database = "master"
$contentRootItem = Get-Item -Path "$($database):\content\Master\Home"
##
# Collect all Master component templates
#
$componentTemplates = @()
$componentsRootItem = Get-Item -Path "$($database):\templates\User Defined\Master\Components"
$suvaComponents = Get-ChildItem -Path $componentsRootItem.ProviderPath -Recurse | Foreach {
$Item = $_
If ($Item.TemplateName -eq "Template") {
$componentTemplates = $componentTemplates + @($Item.Name)
}
}
##
# Collect all items below content root item, which meet the following requirements:
# 1. the item is using a Master component template
# 2. the item is stored in a 'Local Data' or 'Shared Data' folder
# 3. the item does not have any referring items
#
$items = Get-ChildItem -Path $contentRootItem.ProviderPath -Recurse | IsCandidateItem -Templates $componentTemplates | HasNoReferrers
##
# Create the report output
#
if($items.Count -eq 0){
Show-Alert "Es wurden keine Datasources gefunden, die nicht (mehr) mit einer Seite/Rendering verknüpft sind."
} else {
$props = @{
Title = "Unreferenzierte Datasources - Resultate"
InfoTitle = "Datasources, die nicht mehr mit einer Seite verknüpft sind"
InfoDescription = "Zeigt die Datasources an, die nicht mehr mit einem Rendering in einer Seite verknüpft sind"
PageSize = 25
}
$items | Show-ListView @props -Property @{Label="Datasource Name"; Expression={$_.DisplayName} },
@{Label="Datasource Type"; Expression={$_.TemplateName} },
@{Label="Created"; Expression={$_.__Created} },
@{Label="Path"; Expression={$_.Paths.ContentPath} }
}
Close-Window
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment