Next and previous document sibling

Mateusz Żebrowski asked on October 27, 2015 07:58

Is it possible to write a macro that will get next and previous siblings of current document?

Correct Answer

David te Kloese answered on October 27, 2015 12:33

Hi,

True that's why I'd write a custom macro... but I managed to write my most Frankenstein-like basic macro ever :)

{%
currentNodeID = CMSContext.CurrentDocument.NodeID;
next = null;
previous = null;

parent = Documents[CMSContext.CurrentDocumentParent.NodeAliasPath]; 
counter = 0;

while (counter < parent.Children.Count)
{
  if ((parent.Children[counter].NodeID == currentNodeID))
      { next = parent.Children[counter + 1]; previous = parent.Children[counter - 1];}
  counter++;
}

return (previous.NodeName + " - " + next.NodeName)
|(user)zbynekh|(hash)e247fa94b7bc411fb43ce5897023e86d5a789c198b13c55faab38dc9be2f5beb%}

Again might not be the best performance or complete, but will get you started if you need just basic macro's...

Greets,

David

1 votesVote for this answer Unmark Correct answer

Recent Answers


David te Kloese answered on October 27, 2015 10:25 (last edited on March 24, 2018 12:15)

Hi Mateusz,

Without writing a custom macro (and solving the problem in code) this was the only way I got it to work using basic macros:

previous: {%Documents[CMSContext.CurrentDocumentParent.NodeAliasPath].Children[Documents[CMSContext.CurrentDocument.NodeAliasPath].NodeOrder - 2] |(user)zbynekh|(hash)6bab48cf0662d125647bdb221820fd4176d5e06cdecd4ac7bcaa6fed9e3544fb%} current: {%Documents[CMSContext.CurrentDocumentParent.NodeAliasPath].Children[Documents[CMSContext.CurrentDocument.NodeAliasPath].NodeOrder - 1] |(user)zbynekh|(hash)442abe6bb33d103bb2de17d7f7bd80e5d57920e5b74ff0f21737746506913675%} next: {%Documents[CMSContext.CurrentDocumentParent.NodeAliasPath].Children[Documents[CMSContext.CurrentDocument.NodeAliasPath].NodeOrder] |(user)zbynekh|(hash)28503d373f54e7755d7da19924132e6a31b9b43c4fd0d39d8c2a28b0475621da%}

What I actually do is get the children of the parent based on the current NodeOrder (-1 or +1)

I Don't think this is very efficient performance wise so if you decide to use this make sure to check for performance and I advise using cache :)

Greets,

David

0 votesVote for this answer Mark as a Correct answer

Mateusz Żebrowski answered on October 27, 2015 11:16

You assumed that first element have NodeOrder=1 second have NodeOrder=2 and so on. It is true unless you change order by moving documents on cms tree.

Example: We create two documents A and B. On the tree A is before B. Now document A have NodeOrder=1 B have NodeOrder=2. When we put B before A we will get: A have NodeOrder=3 B have NodeOrder=2.

Your solution still works but imagine that we will have more then two documents with node orders 2,3,5,6.

0 votesVote for this answer Mark as a Correct answer

Mateusz Żebrowski answered on October 27, 2015 13:04

Yeah I've done it this way. I'm thing there isn't better solution.

0 votesVote for this answer Mark as a Correct answer

Juraj Ondrus answered on October 27, 2015 13:08

I am not sure if you have checked this article out. At least, it could be a good inspiration - in the code you can see what API is used to get the prev/next nodes.

1 votesVote for this answer Mark as a Correct answer

Mitch du Clou answered on August 14, 2017 17:10 (last edited on August 14, 2017 17:10)

  var prev = DocumentContext.CurrentDocument.Parent.Children.FirstOrDefault(x => x.NodeOrder == DocumentContext.CurrentDocument.NodeOrder - 1);

    var next = DocumentContext.CurrentDocument.Parent.Children.FirstOrDefault(x => x.NodeOrder == DocumentContext.CurrentDocument.NodeOrder + 1);
0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.