Sitecore: Search for Items That Don’t Have Any Language Versions Using Powershell

Recently, I have faced with an issue using traditional Powershell commands. I was trying to identify items that did not have any associated language version. There are various ways of how an item can get to this state. I will not get into these cases here, because they are typically specific to the implementation. But, what is certain across all cases is that these items cause problems because they (and their descendants) don’t get published. This causes a lot of confusion between what you see in Experience Editor and on the front-end site – items are not the same.

The problem lies in the format of traditional Powershell commands which are require a -Language param to retrieve specific versions. I specifically needed to identify unversioned items, but it was not so simply:

  • If you leave the language param off, it will use “en” as the default choice
  • If you pass an empty string for this param, it will return all versions of items that have language versions.

That’s great, but I still had not udentifier the items without a language version. If you want to reproduce it, just create a new item, then go to the “Versions” tab and delete the current version.

Here comes the Powershell

Since I could not use Get-ChildItems to get my unversioned items, I had the idea to look for items that have a collective count of all language versions equal to 0. There is a wonderful method  that helps me to achive my goal – GetVersions. It receives at the input bool includeAllLanguages and returns an array of items. This is the perfect method to leverage since I can test for an empty count to determine if it’s one of the unversioned items.

# point this to the root of your website
$folder = "/sitecore/content"

# Where-Object clause could be combined with other filters
# like Name or Template to narrow results for something specific
$items = Get-ChildItem -Path $folder -recurse | `
Where-Object { $_.Versions.GetVersions($true).Count -eq 0 } | `
%{
[PSCustomObject]@{
"ID" = $_.ID
"ItemName" = $_.Name
"Item" = $_
"ItemPath" = $_.Paths.Path
}
}

# print out any results that were found
$items

I hope this work around will save you some time and frustration. As I mentioned, being able to identify these items is critical to resolving the conflict between what you are seeing in the Experience Editor and what is showing on the front-end site.

Enjoy!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s