Expand my Community achievements bar.

Creating Tags in aem Local using TagManager

Avatar

Level 2

Hi team,

 

How can we create Tags in Local AEM using Tagmanger using code

 

If any reference is there, pls provide.

13 Replies

Avatar

Level 2

@Jineet_Vora and @kautuk_sahni Please check and provide if you have solution or any refernce

Avatar

Community Advisor

HI @Venkata_Naga_SaiBu 

Please check

 

import com.day.cq.tagging.Tag;
import com.day.cq.tagging.TagManager;
import org.apache.sling.api.resource.ResourceResolver;

public class CreateTagInAEM {

    public void createTag(ResourceResolver resourceResolver) {
        // Get the TagManager instance
        TagManager tagManager = resourceResolver.adaptTo(TagManager.class);

        // Define the path where you want to create the tag (e.g., "/etc/tags/mytags")
        String tagPath = "/etc/tags/mytags";

        // Define the name of the tag
        String tagName = "NewTag";

        // Create the tag
        Tag newTag = tagManager.createTag(tagPath, tagName, null, false);

        if (newTag != null) {
            // The tag has been created successfully
            System.out.println("Tag created: " + newTag.getTagID());
        } else {
            // Handle the case where the tag creation fails
            System.out.println("Tag creation failed");
        }
    }
}

 TagManger API : https://developer.adobe.com/experience-manager/reference-materials/6-5/javadoc/com/day/cq/tagging/Ta...  



Arun Patidar

Avatar

Level 2

hi @arunpatidar ,

I tried your code, but it is not generating new tag

 

Thanks

Avatar

Community Advisor

Hi @Venkata_Naga_SaiBu 
Make sure you have resourceResolver with proper permissions.

public void createTag(ResourceResolver resourceResolver)

  

Could you please share your code?



Arun Patidar

Avatar

Level 2
Hi @arunpatidar ,

Pls check and let me know if i need to do any changes.
import com.day.cq.tagging.InvalidTagFormatException;
import com.day.cq.tagging.Tag;
import com.day.cq.tagging.TagManager;
import org.apache.sling.api.resource.ResourceResolver;

public class CreateTagInAEM {

public void createTag(ResourceResolver resourceResolver) throws InvalidTagFormatException {
// Get the TagManager instance
TagManager tagManager = resourceResolver.adaptTo(TagManager.class);

// Define the path where you want to create the tag (e.g., "/etc/tags/mytags")
String tagPath = "/content/cq:tags/wknd-shared";

// Define the name of the tag
String tagName = "NewTag"

// Create the tag
Tag newTag = tagManager.createTag(tagPath, tagName, null, false);

if (newTag != null) {
// The tag has been created successfully
System.out.println("Tag created: " + newTag.getTagID());
} else {
// Handle the case where the tag creation fails
System.out.println("Tag creation failed");
}
}
}

Avatar

Community Advisor

Hi,

From where you are calling below methods

public void createTag(ResourceResolver resourceResolver)

 



Arun Patidar

Avatar

Community Advisor

Try below :

 

Create a  Servlet:

 

package com.example.core.servlets;

import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.osgi.service.component.annotations.Component;
import javax.servlet.Servlet;
import javax.servlet.ServletException;

@Component(service = Servlet.class, property = {
    "sling.servlet.methods=GET",
    "sling.servlet.paths=/bin/createTag"
})
public class CreateTagServlet extends SlingSafeMethodsServlet {

    
    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) {
       // Get the resource resolver
ResourceResolver resourceResolver = request.getResourceResolver();

// Access the TagManager
TagManager tagManager = resourceResolver.adaptTo(TagManager.class);

// Create a new tag
String tagName = "YourTagName";
String tagTitle = "Your Tag Title";
String tagDescription = "Your Tag Description";
Tag tag = tagManager.createTag("/etc/tags", tagName, tagTitle, tagDescription);

// Save the changes
resourceResolver.commit();

response.getWriter().print("Tag created successfully.");
    }
}

 


Access it with http://localhost:4502/bin/createTag



Arun Patidar

codes are as below:

 

Interface:



import com.day.cq.tagging.InvalidTagFormatException;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;

import java.io.IOException;

public interface CreateTagServletinter {
void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException, InvalidTagFormatException;
}

tag Creation:



import com.day.cq.tagging.InvalidTagFormatException;
import com.day.cq.tagging.Tag;
import com.day.cq.tagging.TagManager;
import org.apache.sling.api.resource.ResourceResolver;

public class CreateTagInAEM {

public void createTag(ResourceResolver resourceResolver) throws InvalidTagFormatException {
// Get the TagManager instance
TagManager tagManager = resourceResolver.adaptTo(TagManager.class);

// Define the path where you want to create the tag (e.g., "/etc/tags/mytags")
String tagPath = "/content/cq:tags/wknd-shared";

// Define the name of the tag
String tagName = "NewTag";

// Create the tag
Tag newTag = tagManager.createTag(tagPath, tagName, null, false);

if (newTag != null) {
// The tag has been created successfully
System.out.println("Tag created: " + newTag.getTagID());
} else {
// Handle the case where the tag creation fails
System.out.println("Tag creation failed");
}
}
}

 

 

 

Servlet:


import com.day.cq.tagging.InvalidTagFormatException;
import com.day.cq.tagging.Tag;
import com.day.cq.tagging.TagManager;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.apache.sling.api.resource.ResourceResolver;
import org.osgi.service.component.annotations.Component;
import javax.servlet.Servlet;
import java.io.IOException;

@Component(service = Servlet.class, property = {
"sling.servlet.methods=GET",
"sling.servlet.paths=/bin/createTag"
})
public class CreateTagServlet extends SlingSafeMethodsServlet implements CreateTagServletinter {

@Override
public void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException, InvalidTagFormatException {
// Get the resource resolver
ResourceResolver resourceResolver = request.getResourceResolver();

// Access the TagManager
TagManager tagManager = resourceResolver.adaptTo(TagManager.class);

// Create a new tag
String tagName = "YourTagName";
String tagTitle = "Your Tag Title";
String tagDescription = "Your Tag Description";
Tag tag = tagManager.createTag("/content/cq:tags/wknd-shared", tagName, tagTitle, Boolean.parseBoolean(tagDescription));

// Save the changes
resourceResolver.commit();

response.getWriter().print("Tag created successfully.");
}
}





Avatar

Community Advisor

change doPost to doGet method, I corrected now in the reply.

 

public void doPost(

 



Arun Patidar

Avatar

Level 2

Hi @arunpatidar ,

I got this Message , But the tag is not  created in the path which i had given.

Venkata_Naga_SaiBu_0-1698676766195.png

 

Avatar

Administrator

@Venkata_Naga_SaiBu Did you find the suggestions from users helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community.



Kautuk Sahni