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)
#%}

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

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] #%} current: {%Documents[CMSContext.CurrentDocumentParent.NodeAliasPath].Children[Documents[CMSContext.CurrentDocument.NodeAliasPath].NodeOrder - 1] #%} next: {%Documents[CMSContext.CurrentDocumentParent.NodeAliasPath].Children[Documents[CMSContext.CurrentDocument.NodeAliasPath].NodeOrder] #%}

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

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