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.
Solved! Go to Solution.
Views
Replies
Total Likes
@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!
Please check the below sample query:
query {
articleList {
items {
_tags
_metadata {
stringArrayMetadata {
name
value
}
}
}
}
}
Response:
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");
@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!
Hi @skumari, I added the Query Builder implementation for Metadata/Tags in the GitHub Repository. Please check here: https://github.com/MahediSabuj/aem-react-spa/blob/3488028db20beff9cff391e0673aafca77eec731/core/src/...
String assetPath = request.getParameter("assetPath");
Map<String, String> predicateMap = new HashMap<>();
predicateMap.put("1_path", assetPath);
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");
// In case you want to search by metadata/cq:tags property
// predicateMap.put("5_property", "jcr:content/metadata/cq:tags");
// predicateMap.put("5_property.value", "aem-react-spa:us/articles/aem-cloud");
Session session = request.getResourceResolver().adaptTo(Session.class);
QueryBuilder queryBuilder = request.getResourceResolver().adaptTo(QueryBuilder.class);
Query query = queryBuilder.createQuery(PredicateGroup.create(predicateMap), session);
SearchResult result = query.getResult();
JsonObject jsonObject = new JsonObject();
if (result != null && result.getResources() != null) {
Iterator<Resource> resultIterator = result.getResources();
while (resultIterator.hasNext()){
Resource resource = resultIterator.next();
ValueMap valueMap = resource.getValueMap();
String resourcePath = resource.getPath();
String[] tags = valueMap.get("jcr:content/metadata/cq:tags", new String[0]);
JsonArray jsonArray = new JsonArray();
Arrays.stream(tags).forEach(jsonArray::add);
jsonObject.add(resourcePath, jsonArray);
}
}
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().print(jsonObject);
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")
Views
Likes
Replies