Insights

Comparing Sitecore Items Between Master and Web Databases

Content Out of Sync?

At some point in time, you’ll need to know the items that aren’t yet published or items that have different updated date than those that are published. This most often comes about when you, or your clients, are not taking advantage of things such as workflows or are using duplicate item names and thinking it won’t matter. And then someone goes and does a full site publish and bam, stuff not supposed to be published is.

Master versus Web Sitecore Database?

It’s so easy to have this happen, in a development environment because many times, workflows are near the end. They hinder speed of creation. You can see changes a lot more quickly. But it has it’s downfalls, when doing it that way in production. Likewise not following naming standards. For development, you’re far better switching your site to using master than web and still using workflows.

So where do we go from here?

Note: This solution doesn’t work in the XM Cloud space because there, there is no “web” database, but only Edge.

PowerShell to the Rescue

The easiest way to identify all the guilty items is by using PowerShell to generate a report. So what does a simple report look like? Glad you asked. The report below will generate a comma delimited table you can save into a .csv file and open in Excel for easy management.

# Specify the path to the content root, or media root.
$homeItemPath = "/sitecore/content/Tenant/Site/Home"

# Define the function to recursively process child items
function Compare-UpdatedDates($masterItem) {
    $itemPath = $masterItem.Paths.FullPath
    $webItem = Get-Item -Path web:$itemPath -ErrorAction SilentlyContinue
    if ($webItem -eq $null) {
       "$itemPath,N,$($masterItem.ID),$($masterItem.__Updated.DateTime)"
    } else {
      if ($masterItem.__Updated.DateTime -ne $webItem.__Updated.DateTime) {
        # Output item details in both master and web

        "$itemPath,Y,$($masterItem.ID),$($masterItem.__Updated.DateTime),$($webItem.ID),$($webItem.__Updated.DateTime)"
      }
    }  
}

# Output the header
"Item Path,Published?,Master ID,Master Updated Date,Web ID,Web Updated Date"
# Recursively process the child items of the current items
$masterChildItems = Get-ChildItem -Path master:$homeItemPath -Recurse 

$masterChildItems | ForEach-Object {
        Compare-UpdatedDates -masterItem $_
}

Essentially what we’re doing here is getting the full list of all items in the master database under a specific root item. In this case, the home item. You can do this though for media items as well or any other section of content.

From there we’re doing a lookup to see if that item exists in the master database. If it doesn’t, then we know the item is not published and denote it as such. If it does exist, then we check if the Updated date time matches that of the web database. If it doesn’t, then we know that content exists in production.

In Summary

While the script above is incredibly handy for when you’re doing comparisons to perhaps recover from a publishing mishap or as part of migration, I would steer clear of comparing too large of a tree at one time.


Related Links

Meet David Austin

Development Team Lead | Sitecore Technology MVP x 3

📷🕹️👪

David is a decorated Development Team Lead with Sitecore Technology MVP and Coveo MVP awards, as well as Sitecore CDP & Personalize Certified. He's worked in IT for 25 years; everything ranging from Developer to Business Analyst to Group Lead helping manage everything from Intranet and Internet sites to facility management and application support. David is a dedicated family man who loves to spend time with his girls. He's also an avid photographer and loves to explore new places.

Connect with David