" />

Items as resources by Sitecore part 2: reports

When working with items as resources within Sitecore, there are scenarios where you want to know which items have been overwritten and which items contain a specific .dat file. But how do you do that? For example, by creating a “Find overwritten resource items” report in Sitecore PowerShell.

Within Sitecore it is extremely easy to overwrite items stored in a .dat file. Of course you want to prevent this from happening, you do this by making the user aware of this by creating a pop up warning. The ‘how to’ is described in part 1 of this blog series: ‘Items as resources by Sitecore part 1: warnings’ and in the video I created regarding part 1 and part 2 of this blog series.

Despite your set warning it can still happen that an item in your .dat file is overwritten. If you regularly create and release a .dat file, be aware that the changes to previously overwritten items will not be updated in the .dat file. So it is useful to have a report of which items have been overwritten. You can of course use the Sitecore UpdateApp Tool for this. Just put all the .dat files in the app’s Data folder and configure the databases in the ConnectionStrings.config and run Sitecore.UpdateApp.exe clean and check the log file.

sitecore update app log

This method works fine for upgrading. It also cleans up overwritten items without changes and it displays the GUID of the changed field. But it can be easier, safer and faster if you just want a notification. When running the Sitecore UpdateApp Tool, there is a possibility that the .dat files are different, compared to those placed on the running Sitecore environment. This can cause difficulties when the UpdateApp tool runs against the production environment. You can easily prevent this from happening by creating complete reports in Sitecore PowerShell, without the hassle of the Sitecore UpdateApp Tool.

How to create the 'overwritten resource items' PowerShell report for Sitecore 10.2+

We need to find out in the PowerShell report code which resource files there are and then load those .dat files. We also need to check which items are also stored in the sql database.

Within the Sitecore configuration you can find the file paths where one or more .dat files are stored.

item as resource configuration

Within Sitecore 10.2 there are 2 paths for each database available out of the box:

  • $(dataFolder)/items/$(id
  • /sitecore modules/items/$(id)

Sitecore has a LoadFromFiles method in Assembly Sitecore.Data.ResourceItems.ProtobufNet for protobuffing .dat files with a list of file paths as input. However, when multiple files are loaded at once, you will lose the information that tells you which source file an item is stored in. Fortunately, this method also accepts a single file as input, so run the files first and check which items have been overwritten per file.

To determine if an item has been overwritten, you could use the same code as described in part 1 of this blog series. For this script we have another way. Just because we have loaded the source file itself, we can look in the database. This can be done via the sqlDataProvider. Note: a Prefetch data cache flush is desirable and include in the script, otherwise you will miss recent changes.

The ‘find overwritten resource items’ Sitecore PowerShell Report script:

To add the report to the Start Menu, follow the next steps:

  1. In Sitecore PowerShell ISE, click on New, New Module, give the module a name, and select ‘Report - Start Menu’ as Integration points to create.
  2. Copy the script in PowerShell ISE and Save as ‘Find overwritten resource items’ in the folder ‘module name/Reports’.

You can also give the report a nice icon in the content editor: go to the “Find overwritten resource items” item in the folder: /sitecore/system/Modules/PowerShell/Script Library/ in your module in the Reports folder.

The “Find overwritten resource items” report should now be in the menu called: Reporting Tools / PowerShell Reports.

Sitecore report menu

When you run the report you can select the .dat files you want to check. Here, you can choose to view all resource items in the report or only the overwritten ones.

Find overwrittes resource items option

In the actual report you can sort and export the data, see if the resource item is overwritten, and see in which resource file it is stored.

find overwritten resource items report

If you want to restore the overwritten resource items you need to delete the items from the sql database. To do so, you can use this script written by fellow Sitecore developer Maarten for removing IAR items from the database.