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.
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.
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: