1

I am using Sitecore 9.0.2 and I have a listing page created by custom code, not sxa components. I am trying to get items using the context database and then querying its descendants using

Item Hotels = DB.GetItem("itemID");
List<Item> HotelsItems = Hotels.Axes.GetDescendants().ToList()

I have only one version of each item but upon querying, apparently another version of the item is created in the context language with empty fields. Is there any reason this might be happening?

4
  • 2
    Are you sure that new versions are created? Have you checked in Sitecore Content Tree? There may be items without versions returned with your call. Always check if item.Versions.Count > 0
    – Marek Musielak
    Dec 5, 2019 at 10:43
  • The version isn't created in sitecore content tree, but is returned in the query and displayed eventually in the listing page
    – BasantA
    Dec 5, 2019 at 10:59
  • If you're in Experience Edit mode, Sitecore will return empty versions for editing if no version exists yet. Try calling the page with ?sc_mode=normal and see if they're still there.
    – Mark Lowe
    Dec 5, 2019 at 11:47
  • I've seen this before on multiple Sitecore projects using that version of Sitecore. I've even seen it happen in some modules written by the Hedgehog team. The common denominator seems to be when multiple developers are working on a project. I believe it's either a Sitecore bug or a very common misconfiguration. Dec 5, 2019 at 12:46

1 Answer 1

1

When you call

 item.Axes.GetDescendants()

you may get all the descendants back and it doesn't matter if descendant has a version in given language or not. Item exists, so it IS returned.

To make sure you only select items which have version in your language, use item.Versions.Count property:

Item Hotels = DB.GetItem("itemID");
List<Item> HotelsItems = Hotels.Axes.GetDescendants().Where(i => i.Versions.Count > 0).ToList()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.