javaaemsling

how can we retrieve the tag properties in the backend?


In AEM, I have created the tags under the namespace dedicated to products and further structured them in a nested manner beneath the products tag. These tags have been applied to assets within the system. Now, I am trying to access these tags in the backend for specific purposes. Is it possible to retrieve the tag properties in the backend?

Since I am new to this field, I don't have much idea about how to handle it in the backend. Can anyone please help with this?


Solution

  • Yes, it is possible to retrieve tag properties in the backend of Adobe Experience Manager (AEM). AEM provides APIs and services that allow developers to access metadata associated with assets, including tags. You can use the TagManager API or the AssetManager API to retrieve tag properties programmatically.

    Normally, the tags folder will be stored under /content/cq:tags. (Use this to utilize the path of the tag in the backend.)

    Try this code

    TagManager tagManager = request.getResourceResolver().adaptTo(TagManager.class);
    //Get the Tag Resource by specifying the path
    Tag tags = tagManager.resolve("/content/cq:tags/wknd-assets/products");
    // Check if tags exist.
    if (tags != null) {
        // Get the Iterator for child tags.
        Iterator < Tag > tagIterator = tags.listChildren();
    
        JsonArrayBuilder arrayBuilder = Json.createArrayBuilder();
        while (tagIterator.hasNext()) { // Iterate over child tags.
            Tag tag = tagIterator.next();
    
            JsonObjectBuilder objectBuilder = Json.createObjectBuilder();
            objectBuilder.add("Title", tag.getTitle());
            objectBuilder.add("Tag ID", tag.getTagID());
            arrayBuilder.add(objectBuilder);
        }
        response.getWriter().write(arrayBuilder.build().toString());
    } else {
        response.getWriter().write("Resource Not found");
    }
    

    Please import the required packages.