Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sitecore Axes.IsDescendantOf / Axes.IsAncestorOf - Inclusive?

Is there a reason or something that I am missing that has Sitecore return true for both Item.Axes.IsDescendantOf() and Item.Axes.IsAncestorOf()?

var test =
Sitecore.Context.Database.GetItem("{862B466A-079B-40E7-8661-FC064EC28574}");  
Response.Write(test.Axes.IsAncestorOf(test));  
Response.Write(test.Axes.IsDes(test));

//True
//True

Edit: Anyone whom may stumble across this answer looking for non-inclusive IsAncestorOf or IsDescendantOf, below are a couple examples where I need to find the highest level elements in a multi-select field for news categories.

newsCategories
   .Where(x => newsCategories
                       .Any(y => x != y && !x.Axes.IsDescendantOf(y)))

and

newsCategories
   .Where(x => newsCategories
                       .Any(y => x != y && !x.Axes.IsDescendantOf(y)))
like image 616
al3xnull Avatar asked Dec 16 '22 00:12

al3xnull


2 Answers

I would have to believe that the methods in Sitecore should not be named IsAncestorOf and IsDescendantOf. Based on your finding, and quickly looking at the Sitecore code, the methods should really be named IsAncestorOrSelf and IsDescendantOrSelf.

For this example,

Sitecore.Data.Items.Item x = //some item;
Sitecore.Data.Items.Item y = //some item;
x.Axes.IsAncestorOf(y)
x.Axes.IsDescendantOf(y)

The IsAncestorOf method is comparing x.ID == y.ID. It will then keep comparing x.ID to the ID of y's parent, which is why it should be named IsAncestorOrSelf.

The IsDescendantOf method is comparing if the LongID path of x starts with the LongID path of y. Being that a string always starts with the same string we again see that this method should be named IsDescendantOrSelf.

FYI, A LongID path looks like this /{11111111-1111-1111-1111-111111111111}/{0DE95AE4-41AB-4D01-9EB0-67441B7C2450}/{110D559F-DEA5-42EA-9C1C-8A5DF7E70EF9} and I suspect is used rather than /sitecore/content/home in order to get around the fact that sitecore allows siblings to have the same name.

like image 71
Sean Kearney Avatar answered Dec 28 '22 09:12

Sean Kearney


You are hitting a "fail safe" in the Sitecore logic. It is because you are checking the same item against it self. You need to perform this with 2 different items to produce any result.

like image 21
Sandbeck Avatar answered Dec 28 '22 10:12

Sandbeck