Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

Not able to get the array or tags using TagManager

prachiz
Level 2
Level 2

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.

1 Accepted Solution
Vijayalakshmi_S
Correct answer by
Community Advisor
Community Advisor

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);

 

View solution in original post

9 Replies
Theo_Pendle
Community Advisor
Community Advisor

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
Correct answer by
Community Advisor
Community Advisor

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);

 

View solution in original post

prachiz
Level 2
Level 2

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!

Vijayalakshmi_S
Community Advisor
Community Advisor

Hi,

 

Good to know that it worked.

For getting resources tagged with specific tags, in the tagnames array, use the values as is available in the jcr:content node of respective content path. 

Eg. Pen is tagged in the path say, /content/orders/xyx.

Navigate to  /content/orders/xyx/jcr:content and look for cq:tags property and use the same value as it is available there. (That is the actual tag Id of the respective tag)

prachiz
Level 2
Level 2

I tried using specific path for a specific order2 as below

Resource tagpath3=resolver.getResource("/content/orders/order2");

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

it's returning the tag node, However, I want to get list of all the order nodes containing specific tag, "Pencil". 
Is there any way I could do that using find method?

 

Thanks!

Vijayalakshmi_S
Community Advisor
Community Advisor

The snippet in your previous comment will help get the same. Just that you need to use the exact tagId value in tagnames array. Looks like you have used the tag title - Pen/ Pencil..

TagId value should be different with namespace.

String[] tagnames={"Pen","Desk"}; // Use the exact Tag Id values in this array. 
RangeIterator<Resource> it = tagManager.find("/content/orders",tagnames)

Example:

In the image below, tag Id is we-retail:activity/biking

vijis31358935_0-1586214583342.png

 

Vijayalakshmi_S
Community Advisor
Community Advisor
Hi, Can you share the property of this tag (screenshot of Pen tag from being in tags path - Hide if there are any project specific details)
prachiz
Level 2
Level 2
It's working now after adding the mixin type cq:Taggable to my order nodes. Thanks @Vijayalakshmi_S for the help!