Not able to get the array or tags using TagManager | Community
Skip to main content
Level 3
April 6, 2020
Solved

Not able to get the array or tags using TagManager

  • April 6, 2020
  • 2 replies
  • 8589 views

Hi,

I have created the tags under 

/content/cq:tags/excel-challenge

When I try to get the list of all the tags under the above path, I get ArrayIndexOutOfBoundsException

Resource tagpath=resolver.getResource("/content/cq:tags/excel-challenge");

Tag[] tags=tagManager.getTags(tagpath);

logger.info("Length of tags:"+tags.length);

 Thanks in advance.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Vijayalakshmi_S

Hi,

In order to get the list of all tags under the given tag path, we need to resolve the respective path as "Tag" using "TagManager", followed by listChildren() or listAllSubTags() for immediate/direct child tags or tags of utmost depth respectively. 

 

Tag tag = tagManager.resolve("/content/cq:tags/we-retail");
Iterator<Tag> tagItr = tag.listChildren();

 

Below line that you have used in your code will return all the tags set on the given resource (not the child tags from the given tag path)

Eg. Tags set on page or asset resource. 

Tag[] tags=tagManager.getTags(tagpath);

 

2 replies

Theo_Pendle
Level 8
April 6, 2020

Hi, 

I think you might be using the wrong method.

  1. TagManager.getTags(Resource resource) is used to get the tags which are set on a resource (for example a page)
  2. TagManager.getNamespaces() is used to retrieve tag namespaces as a Tag[], from there you can find the child tags of a namespace by using Tag.listChildren().

If you want to get a list of all the tags you created under /content/cq:tags/excel-challenge, you should be using the second one 👍

Vijayalakshmi_S
Vijayalakshmi_SAccepted solution
Level 10
April 6, 2020

Hi,

In order to get the list of all tags under the given tag path, we need to resolve the respective path as "Tag" using "TagManager", followed by listChildren() or listAllSubTags() for immediate/direct child tags or tags of utmost depth respectively. 

 

Tag tag = tagManager.resolve("/content/cq:tags/we-retail");
Iterator<Tag> tagItr = tag.listChildren();

 

Below line that you have used in your code will return all the tags set on the given resource (not the child tags from the given tag path)

Eg. Tags set on page or asset resource. 

Tag[] tags=tagManager.getTags(tagpath);

 

aembeeAuthor
Level 3
April 6, 2020

Thanks @vijayalakshmi_s for the help!

I'm able to get the list of tags now 🙂

I have a few nodes under content/orders which contains jcr property cq:tags.

Is there any way I could get the list of nodes tagged with specific tags, e.g., Pen? I tried using find method, but it's giving null iterator

String[] tagnames={"Pen","Desk"};
RangeIterator<Resource> it = tagManager.find("/content/orders",tagnames);

Thanks!