Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

Update AEM Tags name with lower case

Avatar

Level 5

Hi,

I am using AEM 6.1 and I have created more then 200 tags with upper case letter in my application ( example:Tag Title: Buy a car, Tag name: Buy-a-car).

I need to follow the correct naming convention and convert the tag name with lower case like :Tag name should be " buy-a-car". Is there any way to convert all the tag name with lower case without deleting or recreating new one or can i run any query to update the name.

Please suggest.

Regards,

Sunil

1 Accepted Solution

Avatar

Correct answer by
Level 9

Here is the fiddle script to convert all tag names jcr:title to lowercase. Modify according to your requirement

package apps.acs_002dtools.components.aemfiddle.fiddle;

import com.day.cq.search.*;

import com.day.cq.wcm.api.*;

import com.day.cq.dam.api.*;

import org.apache.sling.api.*;

import org.apache.sling.api.resource.*;

import org.apache.sling.api.servlets.*;

import java.io.IOException;

import javax.jcr.*;

import java.util.*;

public class fiddle extends SlingAllMethodsServlet {

    @Override

    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException {

        // Code here

        //response.getWriter().println("Hello from " + request.getResource().getPath());

        Resource resource = request.getResourceResolver().getResource("/content/cq:tags/we-retail");

       

Session session =  request.getResourceResolver().adaptTo(Session.class);

        if (resource != null) {

            // Writing HTML in servlets is usually inadvisable, and is better suited to be provided via a JSP/Sightly template

            // This is just an example.

           

           if (resource != null) {

Iterator<Resource> linkResources = resource.listChildren();

while (linkResources.hasNext()) {

Resource childResource = linkResources.next();

try{

Node childNode = childResource.adaptTo(Node.class);

String tagName = childNode.getProperty("jcr:title").getString();

childNode.setProperty("jcr:title",tagName.toLowerCase());

session.save();

response.getWriter().write(""+childNode.getProperty("jcr:title").getString());

   

}catch(RepositoryException re){}

}

}

            // By Default the 200 HTTP Response status code is used; below explicitly sets it.                   

            response.setStatus(SlingHttpServletResponse.SC_OK);

        } else {

            // Set HTTP Response Status code appropriately

            response.setStatus(SlingHttpServletResponse.SC_INTERNAL_SERVER_ERROR);

            response.getWriter().write("ERROR");

        }

      

    }

}

View solution in original post

4 Replies

Avatar

Employee

You can do the following :

- create a servlet  and using QueryManager or any search API to find all tags like (1)

- Iterate over the result set and use java string api to make the items in the resultset to lowercase

- Persist the items back to same location

(1) - select * from [cq:Tag] where isdescendantnode([/etc/tags])

Avatar

Level 10

You can quickly write a small fiddle script using ACS tools without adding this code your source repo

https://adobe-consulting-services.github.io/acs-aem-tools/features/aem-fiddle/index.html

Avatar

Correct answer by
Level 9

Here is the fiddle script to convert all tag names jcr:title to lowercase. Modify according to your requirement

package apps.acs_002dtools.components.aemfiddle.fiddle;

import com.day.cq.search.*;

import com.day.cq.wcm.api.*;

import com.day.cq.dam.api.*;

import org.apache.sling.api.*;

import org.apache.sling.api.resource.*;

import org.apache.sling.api.servlets.*;

import java.io.IOException;

import javax.jcr.*;

import java.util.*;

public class fiddle extends SlingAllMethodsServlet {

    @Override

    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException {

        // Code here

        //response.getWriter().println("Hello from " + request.getResource().getPath());

        Resource resource = request.getResourceResolver().getResource("/content/cq:tags/we-retail");

       

Session session =  request.getResourceResolver().adaptTo(Session.class);

        if (resource != null) {

            // Writing HTML in servlets is usually inadvisable, and is better suited to be provided via a JSP/Sightly template

            // This is just an example.

           

           if (resource != null) {

Iterator<Resource> linkResources = resource.listChildren();

while (linkResources.hasNext()) {

Resource childResource = linkResources.next();

try{

Node childNode = childResource.adaptTo(Node.class);

String tagName = childNode.getProperty("jcr:title").getString();

childNode.setProperty("jcr:title",tagName.toLowerCase());

session.save();

response.getWriter().write(""+childNode.getProperty("jcr:title").getString());

   

}catch(RepositoryException re){}

}

}

            // By Default the 200 HTTP Response status code is used; below explicitly sets it.                   

            response.setStatus(SlingHttpServletResponse.SC_OK);

        } else {

            // Set HTTP Response Status code appropriately

            response.setStatus(SlingHttpServletResponse.SC_INTERNAL_SERVER_ERROR);

            response.getWriter().write("ERROR");

        }

      

    }

}

Avatar

Level 1

Even though I have contents, I getting 0 results