Need java code to convert the page names to lower case | Community
Skip to main content
This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
leeaslingAccepted solution
Level 8
Sep 2, 2016 5:01 PM UTC

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

joerghoh
Employee
May 2, 2018 8:22 PM UTC

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

Translate
viveksachdeva
New Participant
May 2, 2018 5:19 AM UTC

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

Translate
pradeep_kumarr1
New Participant
May 2, 2018 4:01 AM UTC

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

Translate
kautuk_sahni
Employee
Sep 5, 2016 5:05 AM UTC

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

Translate
Kautuk Sahni
leeaslingAccepted solution
Level 8
Sep 2, 2016 5:01 PM UTC

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.

Translate
joerghoh
Employee
Sep 2, 2016 4:42 PM UTC

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

Translate
lakshmi_raghava
Level 3
Sep 2, 2016 4:07 PM UTC
        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.
Translate
lakshmi_raghava
Level 3
Sep 2, 2016 4:07 PM UTC
        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.
Translate