Expand my Community achievements bar.

SOLVED

Need java code to convert the page names to lower case

Avatar

Level 3

Hi All,

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

1 Accepted Solution

Avatar

Correct answer by
Level 8

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.

View solution in original post

8 Replies

Avatar

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

Avatar

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

Avatar

Employee Advisor

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

Avatar

Correct answer by
Level 8

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.

Avatar

Administrator

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

Avatar

Level 1

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

Avatar

Community Advisor

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

Avatar

Employee Advisor

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