Recently, I ran across a problem using traditional Powershell commands. I was trying to identify items that did not have any associated language version. There are various ways that an item can get to this state. I won’t get into these cases here, as they are typically specific to the implementation. But, what’s 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 because what you see in Experience Editor ends up not matching what you see on the front-end site.
The problem is, all the traditional Powershell commands that retrieve items require a -Language param to retrieve specific versions. I specifically needed to identify unversioned items. 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 hadn’t uncovered 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 is a great Powershell work around.
Since I couldn’t 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. My friend CJ helped out by showing me that I can use $item.Versions.GetType() to get the .Net class name of the object I’m working with. This allowed me to decompile this class with Resharper to find what methods it exposes. As it turns out, it has a method GetVersions(bool includeAllLanguages) that 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. Here’s the final code block that CJ and I ended up with.
<h1>point this to the root of your website</h1> $folder = "/sitecore/content" <h1>you can combine the Where-Object clause with other tests</h1> <h1>like Name or Template if you are looking for something specific</h1> $items = Get-ChildItem -Path $folder -recurse | <code>Where-Object { $_.Versions.GetVersions($true).Count -eq 0 } |</code> %{ [PSCustomObject]@{ "ID" = $<em>.ID "ItemName" = $</em>.Name "Item" = $_ "ItemPath" = $_.Paths.Path } } <h1>this will print out any results that it finds</h1> $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.
Happy coding! Until next time.
Anastasiya
Very cool. Thanks for sharing. Give `Get-Member` a try. It can be used to inspect objects. `$item.Versions | Get-Member`
Pingback: Finding Sitecore Items Without a Language Version – Not a good time for coding