Created: 19 Mar 2023, last update: 9 Nov 2024
Items as resources PowerShell Content Editor Warning
In this blog post, we'll discuss two important topics related to Sitecore CMS.
The first topic is a custom warning that appears in the Sitecore content editor when trying to determine if an item originates from a resource file. In a previous blog, a solution had been built to address this warning see Items-as-resources-by-sitecore-part-1, but it was not able to detect overwritten items. However, with Sitecore 10.3+, it is now possible to detect overwritten items very easy and without having to extend the data providers. For more information, check out the documentation provided by Sitecore, Work with items as resources.
The second topic we'll cover is the customization of the Sitecore CMS by uploading custom C# code in DLLs. While this used to be a common approach, it is now less desirable with Sitecore XM Cloud. However, there's no need to worry as a lot can be solved with PowerShell.
By optimizing these two topics, we hope to provide you with a better understanding of how to work with Sitecore CMS and avoid potential issues when customizing it.
Detecting a resource item with Sitecore PowerShell example:
$item = Get-Item -Path master: -Query "/sitecore/system/"
$aItemLocation = $item.Database.DataManager.DataSource.GetItemLocations($item.ID)
[Sitecore.Data.DataProviders.ItemLocations.AggregatedItemLocationsExtensions]::IsResource($aItemLocation)
[Sitecore.Data.DataProviders.ItemLocations.AggregatedItemLocationsExtensions]::IsSql($aItemLocation)
[Sitecore.Data.DataProviders.ItemLocations.AggregatedItemLocationsExtensions]::IsOverridden($aItemLocation)
#get a list of all overwritten items:
$item.Database.DataManager.DataSource.GetOverriddenItems()
Using the above PowerShell as a base and using the Content Editor options of Sitecore PowerShell, we can now build the Content Edit Warning with Sitecore PowerShell stored in a Sitecore Item, without deploying custom code and files. Using Sitecore PowerShell Extensions (SPE) can be considered a low code approach to developing Sitecore CMS customalizations.
$sitecoreversion = [Sitecore.Configuration.About]::Version
if ($sitecoreversion.StartsWith('10.1'))
{
#Sorry this is only for Sitecore 10.2+ and XMCloud
exit
}
$icon = $PSScript.Appearance.Icon
$iconUrl = [Sitecore.Resources.Images]::GetThemedImageSource($icon)
$title = "Item as resource"
$text = "This item is an item as resources"
$item = Get-Item -Path .
$aItemLocation = $item.Database.DataManager.DataSource.GetItemLocations($item.ID)
$isResource = [Sitecore.Data.DataProviders.ItemLocations.AggregatedItemLocationsExtensions]::IsResource($aItemLocation)
$isOverridden = [Sitecore.Data.DataProviders.ItemLocations.AggregatedItemLocationsExtensions]::IsOverridden($aItemLocation)
if ( $isOverridden )
{
$text = "This item is an Overridden item as resources"
}
if ($isResource) {
$warning = $pipelineArgs.Add()
$warning.Title = $title
$warning.Text = $text
}
This code and also a nice report with restore option can be found on GitHub.