Need java code to convert the page names to lower case | Community
Skip to main content
lakshmi_raghava
Level 3
September 2, 2016
Solved

Need java code to convert the page names to lower case

  • September 2, 2016
  • 8 replies
  • 5431 views

Hi All,

is there any API or java code to convert the page names to lower case pragmatically. 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by leeasling

I highly recommend using the Groovy Console (https://github.com/Citytechinc/cq-groovy-console) for this.

You can write a script like below:

getNode('/content/path/en').recurse { node -> rename node to node.name.toLowerCase() save() }

And that'll take care of it for you.

8 replies

lakshmi_raghava
Level 3
September 2, 2016
        I need to convert page names not page title.  For page title, I know the approach,  I need for page names nothing but page paths.
lakshmi_raghava
Level 3
September 2, 2016
        I need to convert page names not page title.  For page title, I know the approach,  I need for page names nothing but page paths.
joerghoh
Adobe Employee
Adobe Employee
September 2, 2016

Hi,

the page names are actually the name of the jcr nodes (which have a primary type of "cq:page"); so either traverse the tree you want to convert or use JCR SQL to determine the pages, and then use the JCR API to rename the pages (using session.move()).

Jörg

leeaslingAccepted solution
Level 8
September 2, 2016

I highly recommend using the Groovy Console (https://github.com/Citytechinc/cq-groovy-console) for this.

You can write a script like below:

getNode('/content/path/en').recurse { node -> rename node to node.name.toLowerCase() save() }

And that'll take care of it for you.

kautuk_sahni
Community Manager
Community Manager
September 5, 2016

Hi 

Option 1:-

Using JCR API,

void rename(Node node, String newName) throws RepositoryException 
    {
        node.getSession().move(node.getPath(), node.getParent().getPath() + "/" + newName.toLowerCase());  //  
       
        node.getSession().save();
    }

Link:- http://stackoverflow.com/questions/19653046/how-to-rename-a-cq5-node-name-using-groovy-console

Option 2:- As mentioned by Lee, Use of Groovy console is good option..

//Move/Renaming in Groovy console

     getPage("/content/project").recurse { page ->
        def content = page.node
        if (content && "/apps/project/templates/club" == content.get("cq:template")) {
            content.set("cq:template", "/apps/project/templates/noclubs")
            println page.path
        }
    }
    save()

// to lower case 

    nodeName.toLowerCase()

Documentation link:- http://groovy-lang.org/single-page-documentation.html

I hope this will help you.

 

Thanks and Regards

Kautuk Sahni

Kautuk Sahni
pradeep_kumarr1
May 2, 2018

Can you give a sample example for renaming the node using groovy script

viveksachdeva
Community Advisor
Community Advisor
May 2, 2018

You can use something like code below:

resourceResolver.getResource("/content/geometrixx").listChildren().each {

    Node node = it.adaptTo(Node.class)

     node.getSession().move(node.getPath(), node.getParent().getPath() + "/" + node.name.toLowerCase());  // 

     node.getSession().save();  

}

P.S. : When you change node name, any place that you were referring it on also needs to change...

joerghoh
Adobe Employee
Adobe Employee
May 2, 2018

Please be also aware of the runtime implications of this. You are moving nodes, not just renaming.