Groovy script not working as expected - Trying to update cq:tags | Community
Skip to main content
Level 2
May 1, 2025
Solved

Groovy script not working as expected - Trying to update cq:tags

  • May 1, 2025
  • 2 replies
  • 611 views

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?

Best answer by SantoshSai

@priyanair1 

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" }

2 replies

SantoshSai
Community Advisor
Community Advisor
May 1, 2025

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!

Santosh Sai
Level 2
May 1, 2025

@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

SantoshSai
Community Advisor
SantoshSaiCommunity AdvisorAccepted solution
Community Advisor
May 1, 2025

@priyanair1 

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" }
Santosh Sai
AmitVishwakarma
Community Advisor
Community Advisor
May 1, 2025

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