How to move a node to top in a container programatically? | Community
Skip to main content
Level 2
February 2, 2025
Solved

How to move a node to top in a container programatically?

  • February 2, 2025
  • 3 replies
  • 585 views

I have a container in which three nodes are present. I want to rearrange the 2nd node to the top position in the container. How can I do this programmatically?

Best answer by aanchal-sikka

@sahildh1 

you can utilize the orderBefore API to achieve the same. 

private void orderAsFirstChild(String childName, Node parentNode) throws RepositoryException { if (parentNode.hasNode(childName)) { // find current first child name String firstChildName = Optional.ofNullable(parentNode.getNodes()) .map(NodeIterator::nextNode) .map(node -> { try { node.getName(); } catch (RepositoryException e) { e.printStackTrace(); } return (String) null; }) // .orElse(null); parentNode.orderBefore(childName, firstChildName); } }

 Src: https://stackoverflow.com/questions/55239195/programmatically-arranging-contents-in-aem-6-4

3 replies

daniel-strmecki
Community Advisor and Adobe Champion
Community Advisor and Adobe Champion
February 2, 2025

Hi @sahildh1,

I've never tried it, but there seems to be an API for re-ordering: https://developer.adobe.com/experience-manager/reference-materials/spec/jsr170/javadocs/jcr-2.0/index.html

 

Good luck,

Daniel

aanchal-sikka
Community Advisor
aanchal-sikkaCommunity AdvisorAccepted solution
Community Advisor
February 3, 2025

@sahildh1 

you can utilize the orderBefore API to achieve the same. 

private void orderAsFirstChild(String childName, Node parentNode) throws RepositoryException { if (parentNode.hasNode(childName)) { // find current first child name String firstChildName = Optional.ofNullable(parentNode.getNodes()) .map(NodeIterator::nextNode) .map(node -> { try { node.getName(); } catch (RepositoryException e) { e.printStackTrace(); } return (String) null; }) // .orElse(null); parentNode.orderBefore(childName, firstChildName); } }

 Src: https://stackoverflow.com/questions/55239195/programmatically-arranging-contents-in-aem-6-4

Aanchal Sikka
kautuk_sahni
Community Manager
Community Manager
February 10, 2025

@sahildh1 Did you find the suggestions helpful? Please let us know if you need more information. If a response worked, kindly mark it as correct for posterity; alternatively, if you found a solution yourself, we’d appreciate it if you could share it with the community. Thank you!

Kautuk Sahni