Hi team,
How can we create Tags in Local AEM using Tagmanger using code
If any reference is there, pls provide.
Views
Replies
Total Likes
@Jineet_Vora and @kautuk_sahni Please check and provide if you have solution or any refernce
You can refer to this documentation for instructions on creating tags using Tag Manager API.
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...
Hi @Venkata_Naga_SaiBu
Make sure you have resourceResolver with proper permissions.
public void createTag(ResourceResolver resourceResolver)
Could you please share your code?
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");
}
}
}
Hi,
From where you are calling below methods
public void createTag(ResourceResolver resourceResolver)
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
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.");
}
}
change doPost to doGet method, I corrected now in the reply.
public void doPost(
@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.
Views
Replies
Total Likes
Views
Likes
Replies
Views
Likes
Replies
Views
Likes
Replies
Views
Likes
Replies