Reading meta tag values of an asset | Community
Skip to main content
October 16, 2015
Solved

Reading meta tag values of an asset

  • October 16, 2015
  • 10 replies
  • 5094 views

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

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 ogill

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

10 replies

Techaspect_Solu
Level 7
October 16, 2015

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.

edubey
Level 10
October 16, 2015

Hi,

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

October 16, 2015

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

ogillAdobe EmployeeAccepted solution
Adobe Employee
October 16, 2015

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

October 16, 2015

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

edubey
Level 10
October 16, 2015

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 

Adobe Employee
October 16, 2015

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]);
    }
%>

October 16, 2015

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

edubey
Level 10
October 16, 2015

Attached missing screenshots

edubey
Level 10
October 16, 2015

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