Expand my Community achievements bar.

SOLVED

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

Avatar

Level 2

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?

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

@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

View solution in original post

3 Replies

Avatar

Level 10

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/inde...

 

Good luck,

Daniel

Avatar

Correct answer by
Community Advisor

@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

Avatar

Administrator

@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