Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
SOLVED

Reading meta tag values of an asset

Avatar

Former Community Member

Hi,

   I am trying to read all meta tag values added to an asset.I used the following code snippet to read

    1.Reading  tags by the following method

       asset.getMetadatavalue("cq:tags")

    2. Having an array of shorthand tag ids added. 

    3. Used the for loop to iterate over tag ids and used "resolve" method to get the tag object.

        tagManager = resolver.adaptTo(TagManager.class);
        Tag custTag = tagManager.resolve(t1);

    4. When trying to read its values,it is breaking out from the second tag onward. Getting proper values for the first tag and null values for the second one

         custTag.getName();custTag.getPath();

   5. Length of a tag array is coming properly.

 

Could you please help me on this

 

Regards,

Kirithi

1 Accepted Solution

Avatar

Correct answer by
Employee

Please review section "GETTING AND SETTING TAGS" of [1]

     
1
2
3
4
5
// Getting the tags of a Resource:
Tag[] tags = tagManager.getTags(resource);
 
// Setting tags to a Resource:
tagManager.setTags(resource, tags);

Regards,

Opkar

 

[1] https://docs.adobe.com/docs/en/cq/5-6-1/developing/tagging.html

View solution in original post

10 Replies

Avatar

Level 7

Hi,

The reason might be tags aren't available in current instance. 
Tag custTag = tagManager.resolve(t1);
The TagManager will return null value when the tag is not available.
Steps to fix this:
1. Check whether the tags which are stored in the asset properties are available in tagging console or not . (current instance)
2. If they are not available,please activate the tags from author instance. 

This mighe be the solution.

Avatar

Level 10

Hi,

Can you please share the code block to get things more clear. May then I could help you better.

Avatar

Former Community Member

Hi,

    Please find below the code snippet

 

String tagStr = aseetStr.getMetadataValue("cq:tags"); tagArray = tagStr.split(","); for(String t1:tagArray) { LOGGER.info("<<<<<tagArray length>>>>>>"+t1); tagManager = resolver.adaptTo(TagManager.class); Tag custTag = tagManager.resolve(t1); LOGGER.info("Tag Title---->>>>"+custTag.getTitle()); }

   Able to access tag values up to one level irrespective of number of tags have been added.

Thanks!

 

 

Kirithi

Avatar

Correct answer by
Employee

Please review section "GETTING AND SETTING TAGS" of [1]

     
1
2
3
4
5
// Getting the tags of a Resource:
Tag[] tags = tagManager.getTags(resource);
 
// Setting tags to a Resource:
tagManager.setTags(resource, tags);

Regards,

Opkar

 

[1] https://docs.adobe.com/docs/en/cq/5-6-1/developing/tagging.html

Avatar

Former Community Member

Hi,

    I followed the above approach but length of tag is not coming properly.

    It is returning zero.

    So I directly accessed through its property

   

String tagStr = aseetStr.getMetadataValue("cq:tags");

 

   Thanks!

 

Kirithi

Avatar

Level 10

No Worries, Please use below code to get things done. Its workings in my AEM.

<%@include file="/libs/foundation/global.jsp"%> <jsp:directive.page import="com.day.cq.dam.api.Asset, com.day.cq.tagging.TagManager,com.day.cq.tagging.Tag " /> <% try { Resource r = resourceResolver.getResource("/content/dam/geometrixx/drm/with-drm.jpg"); Asset asset = r.adaptTo(Asset.class); String title; Object[] titleArray = null; Object titleObj = asset.getMetadata("cq:tags"); if (titleObj instanceof Object[]) { titleArray = (Object[]) titleObj; } for(Object ob:titleArray) { String a = ob.toString() ; TagManager tagManager=null; tagManager = resourceResolver.adaptTo(TagManager.class); Tag custTag = tagManager.resolve(a); out.print(custTag.getTitle()); } } catch(Exception e) { } %>

For testing purpose I wrote this in JSP its recommended to write this in Java class.

Let me know how it goes 

Avatar

Employee

Hi Kirithi,

here is some code I wrote in Fiddler (part of ACS commons) to extract the tags for an asset.

Regards,

Opkar

<%@include file="/libs/foundation/global.jsp"%><%
%><%@page session="false" contentType="text/html; charset=utf-8" 
    pageEncoding="UTF-8"
    import="org.apache.sling.api.resource.*,
    org.apache.sling.jcr.resource.*,
    com.day.cq.dam.api.*,
    java.util.*,
    javax.jcr.*,
    com.day.cq.search.*,
    com.day.cq.wcm.api.*,
    com.day.cq.dam.api.*,
    com.day.cq.dam.api.metadata.*"%><%

    // Code here
    String resourcePath = "/content/dam/geometrixx/portraits/scott_reynolds.jpg";
    Resource res = resourceResolver.getResource(resourcePath);
    Asset asset = res.adaptTo(Asset.class);

    Map<String, Object> assetMap = asset.getMetadata();
    Object[] obj = (Object[])asset.getMetadata("cq:tags");
    
    for(int x=0;x<obj.length;x++){
    %>
    <br>
    <%
        out.println(obj[x]);
    }
%>

Avatar

Former Community Member

Hi Opkar and Dubey,

                               Thanks:-) ,It is Working now. In order to learn I just want to know why it is not working in my approach.

 

 

 

 

Regards,

Kirithi

Avatar

Level 10

Attached missing screenshots

Avatar

Level 10

Hi Kirithi,

Thanks for confirmation of code, 

I feel couple of reason for not working of code as getMetadataValue(String name) return string but if you see Dam jcr screenshot their property is stored as String[].

This is one helpful link I found : http://dev.day.com/docs/en/cq/current/dam/customizing_and_extendingcq5dam.html

Please see screenshots