AEM Query for retrieving tags value | Community
Skip to main content
New Participant
August 7, 2023
Solved

AEM Query for retrieving tags value

  • August 7, 2023
  • 2 replies
  • 1777 views

Hi Team,

I have a requirement to fetch the value of tags, which is under the metadata of content fragment through json url . So can anyone please provide the query for it.

 

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 Sudheer_Sundalam

@skumari ,

I think you need to write a servlet to run a xpath query to fetch the results. Assuming you need a servlet, this sample code should help you to return result in JSON format

Session session = resourceResolver.adaptTo(Session.class); Map<String, String> predicateMap = new HashMap<>(); predicateMap.put("1_path", /content/dam/content-fragments/bookpedia); predicateMap.put("2_type", "dam:Asset"); predicateMap.put("3_property", "jcr:content/contentFragment"); predicateMap.put("3_property.value", "true"); predicateMap.put("4_orderby", "@jcr:content/jcr:lastModified"); predicateMap.put("4_orderby.sort", "desc"); Query query = queryBuilder.createQuery(PredicateGroup.create(predicateMap),session); SearchResult result = query.getResult(); Iterator<Resource> resultIterator = result.getResources(); Resource resource = null; ValueMap valueMap = null; com.google.gson.JsonObject jsonObject = new JsonObject(); while (resultIterator.hasNext()){ resource = resultIterator.next(); valueMap = resource.getValueMap(); //TO DO //Fetch your properties from the valueMap and add them to the JsonObject as per your needs //jsonObject.addProperty("key", valueFromValueMap); //jsonObject.add("key", anotherJSONObject); } response.setContentType("application/json"); response.setCharacterEncoding("UTF-8"); response.getWriter().print(jsonObject.toString());

Hope this helps!

2 replies

AsifChowdhury
New Participant
August 8, 2023

Hi @skumari 

Using query we can get all tags and then filter out the tags which are assigned to the content fragment programmatically we can get the tags that are assigned to the content fragment by resolving the content fragment path using a resource resolver but we can't get the tags that are assigned to the content fragment directly using a query. For that, we have to resolve the content fragment and then we can get the tags that are assigned to the content fragment from its resource.getValueMap().get("jcr:content/metadata/cq:tags")

Mahedi_Sabuj
New Participant
August 7, 2023

Please check the below sample query:

query { articleList { items { _tags _metadata { stringArrayMetadata { name value } } } } }

Response:

Reference: https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/aem-6-5-graphql-query-to-fetch-tags/td-p/562489 

 

 

Mahedi Sabuj
skumariAuthor
New Participant
August 7, 2023

Hi @mahedi_sabuj 

Thanks for the above information. But we need to add it in code like below. We basically want the response through json.

predicateMap.put("1_path", /content/dam/content-fragments/bookpedia);
predicateMap.put("2_type", "dam:Asset");
predicateMap.put("3_property", "jcr:content/contentFragment");
predicateMap.put("3_property.value", "true");
predicateMap.put("4_orderby", "@jcr:content/jcr:lastModified");
predicateMap.put("4_orderby.sort", "desc");

Sudheer_Sundalam
Sudheer_SundalamAccepted solution
New Participant
August 7, 2023

@skumari ,

I think you need to write a servlet to run a xpath query to fetch the results. Assuming you need a servlet, this sample code should help you to return result in JSON format

Session session = resourceResolver.adaptTo(Session.class); Map<String, String> predicateMap = new HashMap<>(); predicateMap.put("1_path", /content/dam/content-fragments/bookpedia); predicateMap.put("2_type", "dam:Asset"); predicateMap.put("3_property", "jcr:content/contentFragment"); predicateMap.put("3_property.value", "true"); predicateMap.put("4_orderby", "@jcr:content/jcr:lastModified"); predicateMap.put("4_orderby.sort", "desc"); Query query = queryBuilder.createQuery(PredicateGroup.create(predicateMap),session); SearchResult result = query.getResult(); Iterator<Resource> resultIterator = result.getResources(); Resource resource = null; ValueMap valueMap = null; com.google.gson.JsonObject jsonObject = new JsonObject(); while (resultIterator.hasNext()){ resource = resultIterator.next(); valueMap = resource.getValueMap(); //TO DO //Fetch your properties from the valueMap and add them to the JsonObject as per your needs //jsonObject.addProperty("key", valueFromValueMap); //jsonObject.add("key", anotherJSONObject); } response.setContentType("application/json"); response.setCharacterEncoding("UTF-8"); response.getWriter().print(jsonObject.toString());

Hope this helps!