Creating Tags in aem Local using TagManager | 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.

5 replies

Level 2
October 27, 2023

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

Mahedi_Sabuj
Community Advisor
Community Advisor
October 27, 2023

You can refer to this documentation for instructions on creating tags using Tag Manager API. 

https://experienceleague.adobe.com/docs/experience-manager-65/developing/platform/tagging/building.html?lang=en

Mahedi Sabuj
arunpatidar
Community Advisor
Community Advisor
October 27, 2023

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/TagManager.html#createTag-java.lang.String-java.lang.String-java.lang.String-boolean-  

Arun Patidar
Level 2
October 27, 2023

hi @arunpatidar ,

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

 

Thanks

arunpatidar
Community Advisor
Community Advisor
October 27, 2023

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
Level 2
October 30, 2023
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");
}
}
}
kautuk_sahni
Community Manager
Community Manager
October 30, 2023

@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