How to export Sitecore item data in a customized comparable way with „Sitecore Powershell Extensions“ (SPE)?
Sitecore’s strength and weakness is based on it’s granularity in data structure. Hundred thousands of items builds up a solution and sometime it can be enough that only on single field of an item causes a behavior or problem that could not so easily foreseen.
If you run your solution on different staged environments – for example a development, a UAT or a production – there is an additional dimension of miss-configuration injected.
Some of this items can be synchronized over environments by using a tool like Unicorn or TDS. Especially if they keep the same over all destinations. Templates, layouts, renderings, core items can usually handled by this.
If it comes to content or configuration items it can be more difficult. There are also options from the previous mentioned tools (News Items Only configuration in Unicorn).
As a Sitecore developer working in bigger teams with a lot of feature implementation, creating git branches swapping this branches forth and back makes things also not easier.
So something I’m familiar with through the last years of Sitecore development were questions like this:
- How to compare items?
- Why is that not working on environment a) but not on environment b)?
- Locally it worked on my machine, why is it not working on yours?
- Do I have published everything important?
- Can I compare the master database content with the web database content? (Is it published?)
The obvious solution to get an answer to this question is comparison of items between:
- different system/networks
- security restriction layers
- item tree paths
Export data options
To get the ingredients for comparing something you need access to the data (items) in a format you can use (or even better in the format you exactly want to work with).
The items data is accessible by:
- Sitecore UI: The manual approach to compare something, no accurate approach for having fast and correct feedback
- Databases: The Sitecore database structure is not very comfortable to easy compare things so you need a tool like „Razl“, if you have no developer access to the environment this approach is also not possible
- API / WebAPI: Is also depending from network/security accessibility
- Serialized Data: Can be compared with comparing tools developers already know (like kdiff3, BeyondCompare, WinMerge etc.)
- Sitecore Serialization Files: you need access to the file system to copy the data
- Sitecore Packages: can be retrieved as long you have Sitecore UI admin access.
- Tool Serialization (Unicorn YAML): Only data can be compared that is under unicorn control.
One of the problems with all these approaches is that you get the data complete. Complete means you get all values of all fields and data you maybe don’t want to compare.
So you have a bigger size than you want and you have a lot more noise in the data you compare.
Some examples are „Standard Fields“ which are part of all items. Comparing tools of course ignores these field data if it is the same but there are also fields which are often not the same but for you as interpreter it’s not relevant.
- Think about the the „Updated date“ field
- In some circumstances also the „Created Date“ or „Created By“ field, owner
- On a Sitecore Task the „Last Run“ field
- Sometimes the „ID“ itself can differ from system to system
- if you want to compare trees that are note located at the same place in the tree you want to ignore path information
- version data can also be noise, eg: when you compare master database with web database (in web you only have the published version)
To assess the comparison it has advantages to convert the data into your needed format first. So you remove all the data noise in which you are not interested at the beginning.
The following approach could help you in some of the described scenarios.
Sitecore Powershell Extension Approach
The approach looks the following:
- Export item tree structure with the fields you need, of the database of your choice, in the version you want
- Use a PowerShell script on the CM environment to generate this data and download it to your file system
- Do the same export for comparison environment/database/tree
- (Optional) Generate file system tree from export (helps sometimes in the comparison tool to have a better overview)
- Use your favorite file comparison tool to find differences
Preconditions / Requirements
- You have admin account access to the environment CM system
- Sitecore Powershell Extensions are installed on the CM systems
About the script
The script should fulfill the following requirement:
- Have a selection filter for items that should be part of the export
- Have configuration to define „included“ or „excluded“ fields for export of each item
- Have configuration for non field based data that could included to export or not (ID, Path, Template, Version Numbers, Language etc.)
- Export an dynamic object with properties
The script achieving this could look like that:
A very easy example in an empty Sitecore solution:
- I added an own page template type with some fields located in the template helix project folder.
- I also added two content pages under the home item and published one of it.
- Afterwards I executed the example „Compare custom data of the project only“ and it generates me the json files for the database only with the „path“, „version“, „language“ and my custom projects fields I created!
Note: The exported data only contains necessary fields defined in „global configuration“ section of the script (Path, Name, Version) and the custom fields I had created.
The example shows this script gives you some flexibility to the data you want to compare, doesn’t matter if its separated through different environments, projects, tree locations or databases.