Expand my Community achievements bar.

Submissions are now open for the 2026 Adobe Experience Maker Awards.

Mark Solution

This conversation has been locked due to inactivity. Please create a new post.

SOLVED

Get Tags Set on Resource in custom properties

Avatar

Level 2

I want to get list of tags set on resource in custom properties (not in cq:tags).

Values in these properties are set using tagpicker.

 

I tried tagManager.getTagsForSubtree(resource, false) but it's returning values only from cq:tags and tagManager.getTags(resource) returns no tags.

 

Can anyone help in this regards?

 

Thanks

Ritesh

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution

Avatar

Correct answer by
Level 10

Hi,

 

If it is about custom property on the resource, then it is an usual way of retrieving properties from a resource which is via ValueMap.

Then the value retrieved can be resolved as a tag object. 

 

ValueMap rescProps = resource.getValueMap();

String tagStr = rescProps.get("custom propertyname", String.class);

if(null != tagStr){

    Tag actualTag =  tagManager.resolve(tagStr);

}

 

 

View solution in original post

5 Replies

Avatar

Correct answer by
Level 10

Hi,

 

If it is about custom property on the resource, then it is an usual way of retrieving properties from a resource which is via ValueMap.

Then the value retrieved can be resolved as a tag object. 

 

ValueMap rescProps = resource.getValueMap();

String tagStr = rescProps.get("custom propertyname", String.class);

if(null != tagStr){

    Tag actualTag =  tagManager.resolve(tagStr);

}

 

 

Avatar

Level 2

@Vijayalakshmi_S,

Thanks for reply.

However there are multiple custom properties set on a page with tag values. And those can change in future. I don't want to bind the functionality to particular property.

Is there any way to get those tags dynamically with above approach.

 

Thanks,

Ritesh

Avatar

Level 10

Hi @rp5352629

 

On a high level, if it shouldn't be tied to a specific property name, it will involve iteration.

But before I could comment about the apt solution, would like to know exact functionality that you are trying to achieve on the whole, if is fine to share. 

Avatar

Community Advisor and Adobe Champion

@Vijayalakshmi_S,

While using Sling Models, you can utilise the injector specific annotations.

Touch UI Dialogue:

 

<customTags
    jcr:primaryType="nt:unstructured"
    sling:resourceType="cq/gui/components/common/tagspicker"
    fieldLabel="Custom Tags"
    mode="contains"
    name="./customTags"/>

 

Sling Model Java Class:

 

package com.myexamples.models;

import com.day.cq.tagging.Tag;
import com.day.cq.tagging.TagManager;
import lombok.Getter;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.SlingObject;
import org.apache.sling.models.annotations.injectorspecific.ValueMapValue;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

@Model(adaptables = Resource.class,
        defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class ExampleSlingModel {
    @SlingObject
    private ResourceResolver resourceResolver;

    @Getter
    @ValueMapValue
    private String[] customTags;

    @Getter
    private List<Tag> tags = new ArrayList<>();

    @PostConstruct
    private void init() {
        resolveTags();
    }

    private void resolveTags() {
        if (customTags !=  null) {
            TagManager tagManager = resourceResolver.adaptTo(TagManager.class);
            for (int i = 0, n = customTags.length; i < n; i++) {
                Tag tag = tagManager.resolve(customTags[i]);
                if (tag != null) {
                    tags.add(tag);
                }
            }
        }
    }
}

 

Sightly:

 

<sly data-sly-use.example="com.myexamplels.model.ExampleSlingModel"></sly>
<ul data-sly-list.tag="${example.tags}">
    <li>${tag.path}, ${tag.tagID}, ${tag.name}</li>
</ul>

 

Tip: The code above assumes that the current user has view access to the cq:Tags. You can use service-users to obtain the TagManager if permissions are not granted to specific users. 

 Never forget to write JUNIT Tests for your Java Sling Models: https://sourcedcode.com/aem-sling-models-unit-test-junit-4-with-examples

I hope this helps.

Avatar

Level 10

Hi @BrianKasingli ,

Hope it is addressed to the wrong recipient. 

 

Hi @rp5352629,

Based on where exactly you are accessing the resource and hence its properties, the approach differs.

(In case of Sling Models which is via injecting the properties, Servlet/OSGI service, it is via Sling API or JCR API)

In any way, per the original question of getting property value as tag, it is via tagManager.resolve(String tagId) // tagId refers to the property value.