Hi Everyone,
I am trying to use AEM Groovy Console to add a new tag to a page's existing cq:tags property.
Below is my code:
def page = getPage("/content/mysite/en/sample")
def modifiable = page.node
def tagToAdd = "myproject:custom/tag"
def existingTags = modifiable["cq:tags"] ?: []
if (!existingTags.contains(tagToAdd)) {
existingTags << tagToAdd
modifiable["cq:tags"] = existingTags
}
I expect this to add the tag only if it's not already present. But it doesn't work in some cases - especially when the page already has one tag.
Can anyone tell what is wrong?
Solved! Go to Solution.
Views
Replies
Total Likes
Can you try this?
def page = getPage("/content/mysite/en/sample")
def modifiable = page.node
def tagToAdd = "myproject:custom/tag"
// Read existing tags
def existingTagsRaw = modifiable.get("cq:tags")
def existingTags = []
if (existingTagsRaw instanceof String) {
existingTags = [existingTagsRaw]
} else if (existingTagsRaw instanceof String[]) {
existingTags = existingTagsRaw.toList()
}
// Add new tag if it doesn't already exist
if (!existingTags.contains(tagToAdd)) {
existingTags << tagToAdd
modifiable.setProperty("cq:tags", existingTags as String[])
println "Tag added successfully"
} else {
println "Tag already exists"
}
Hi @PriyaNair1,
Try changing the first line where you fetch the tags like this:
def existingTags = modifiable.get("cq:tags") as List ?: []
That should handle cases where cq:tags
is already a list.
Hope that helps!
Views
Replies
Total Likes
@SantoshSai I tried that, but now it’s giving an error when the tag is just a single string.
groovy.lang.MissingMethodException: No signature of method: java.lang.String.each() is applicable for argument types
Views
Replies
Total Likes
Can you try this?
def page = getPage("/content/mysite/en/sample")
def modifiable = page.node
def tagToAdd = "myproject:custom/tag"
// Read existing tags
def existingTagsRaw = modifiable.get("cq:tags")
def existingTags = []
if (existingTagsRaw instanceof String) {
existingTags = [existingTagsRaw]
} else if (existingTagsRaw instanceof String[]) {
existingTags = existingTagsRaw.toList()
}
// Add new tag if it doesn't already exist
if (!existingTags.contains(tagToAdd)) {
existingTags << tagToAdd
modifiable.setProperty("cq:tags", existingTags as String[])
println "Tag added successfully"
} else {
println "Tag already exists"
}
@SantoshSai Thank you, this worked.
Hi @PriyaNair1 ,
Here is a robust Groovy script (well-commented and explained) that adds a tag to a page's cq:tags property using the AEM Groovy Console, ensuring compatibility regardless of whether the property is a single string or a string array:
Groovy Script: Add a New cq:tag Safely in AEM
// Path to the page
def page = getPage("/content/mysite/en/sample")
// Access the underlying JCR node of the page
def modifiable = page.node
// The tag to be added
def tagToAdd = "myproject:custom/tag"
// Get the existing value of 'cq:tags'
def existingTagsRaw = modifiable.get("cq:tags")
// Initialize as empty list
def existingTags = []
// Normalize the tag property to a List<String>
if (existingTagsRaw instanceof String) {
// If only one tag is present as a String
existingTags = [existingTagsRaw]
} else if (existingTagsRaw instanceof String[]) {
// If multiple tags are present
existingTags = existingTagsRaw.toList()
}
// Check if the new tag is already in the list
if (!existingTags.contains(tagToAdd)) {
existingTags << tagToAdd // Append the new tag
// Set the property back as a String array
modifiable.setProperty("cq:tags", existingTags as String[])
println " Tag '${tagToAdd}' added successfully."
} else {
println " Tag '${tagToAdd}' already exists."
}
Handles both String and String[] types (AEM stores cq:tags as multi-value but may return a single string if only one tag is set).
Ensures the updated value is stored as a String[], as expected by AEM for multi-value properties.
Prevents duplicates by checking with .contains() before adding.
Regards,
Amit
Views
Replies
Total Likes
Views
Likes
Replies