Expand my Community achievements bar.

July 31st AEM Gems Webinar: Elevate your AEM development to master the integration of private GitHub repositories within AEM Cloud Manager.
SOLVED

Creating AEM DAM component

Avatar

Level 4

Hi Friends

I am working on creating a DAM component in my project I was following the below url

https://helpx.adobe.com/experience-manager/using/aem-dam-image-components.html

can this example be followed in AEM6.1 as the document says it is for 5.5 and 5.6

When I use the same code snippet in my jsp.

I have my assets in dam/emitra/photogallery

let me know whether I need to mention my DAM asset path as dam.Asset or dam.emitra.photogallery

if I give as dam.emitra.photogallery I am getting assets

'dam.emitra.photogallery' not found

Logs for reference

qb----------->com.day.cq.search.impl.builder.QueryBuilderImpl@57e1389c

query----------->com.day.cq.search.impl.builder.QueryImpl@29b87af7

sr------->com.day.cq.search.impl.result.SearchResultImpl@60365b4f

path------->/jcr:system/jcr:versionStorage/ee/19/c6/ee19c692-acdd-4254-99dd-55fad6b4d050/1.0/jcr:frozenNode

rs------->JcrNodeResource, type=nt:frozenNode, superType=null

asset----------->null

 

Some one can help me

Regards

Pr@veen

1 Accepted Solution

Avatar

Correct answer by
Level 10

If you want to use QUeryBuilder code in a JSP in 6.1 - this works: 

<%@include file="/libs/foundation/global.jsp"%>
<%@page session="false" %>
<%@page import="com.day.cq.tagging.*,com.day.cq.wcm.api.*"%>
 
<%@ page import="java.util.*,
    javax.jcr.*,
    org.apache.sling.api.resource.*,
    org.apache.sling.api.scripting.*,
    org.apache.sling.jcr.api.*,
    com.day.cq.search.*,
    com.day.cq.search.result.*"
 
 %>
 
<%@page import="com.day.cq.dam.api.Asset"%>
 
<% 
SlingRepository slingRep = sling.getService(SlingRepository.class); 
Session session = slingRep.loginAdministrative(null);
QueryBuilder qb ; 
Map<String, String> map = new HashMap<String,String>();
map.put("type", "dam:Asset");
map.put("property", "jcr:content/metadata/dc:format"); 
map.put("property.value", "image/jpeg");
qb=resource.getResourceResolver().adaptTo(QueryBuilder.class);
Query query = qb.createQuery(PredicateGroup.create(map), session);
 
SearchResult sr= query.getResult();
String assetPath=null; 
 
 // iterating over the results
  for (Hit hit : sr.getHits()) {
      String path = hit.getPath();
      Resource rs = resourceResolver.getResource(path);
      Asset asset = rs.adaptTo(Asset.class);     
     assetPath = asset.getPath(); 
%>
<img src="<%= assetPath %>" />
<%

}
%>

View solution in original post

9 Replies

Avatar

Level 10

I am going to try this code on 6.1 and report back the findings, This article was never tested on 6.1 as stated in the summary at the beginning. 

Avatar

Level 4

Scott,

Any updated on AEM6.1 implementation for the above code.

Avatar

Level 10

YOu are correct - the code works on 5.x and throws an null exception on 6.x. We will update the code for AEM 6.

Avatar

Level 10

Found the issue - 5.x was referencing a rendition for that version.

On AEM 6 - change the line 38 to:

<img src = "<%= asset.getRendition("original").getPath()%>" ></img>

And Yes - when using the QUeryBuilder API - you can reference a path where your assets are located. See this AEM Doc topic: 

https://docs.adobe.com/docs/en/cq/5-6-1/dam/customizing_and_extendingcq5dam/query_builder.html

Update - DAM code works on 6 - still an issue with 6.1. Working on it

Avatar

Level 10

 If you are working on 6,1 - its recommended that you do coding a bit different from from 5.x JSPs.

Instead - write a Sightly component that uses QueryBuilder.  QueryBuilder will query for DAM assets and then Sightly displays the result set. See this community article to point you in the right direction: 

https://helpx.adobe.com/experience-manager/using/sightly_querybuilder.html

Avatar

Correct answer by
Level 10

If you want to use QUeryBuilder code in a JSP in 6.1 - this works: 

<%@include file="/libs/foundation/global.jsp"%>
<%@page session="false" %>
<%@page import="com.day.cq.tagging.*,com.day.cq.wcm.api.*"%>
 
<%@ page import="java.util.*,
    javax.jcr.*,
    org.apache.sling.api.resource.*,
    org.apache.sling.api.scripting.*,
    org.apache.sling.jcr.api.*,
    com.day.cq.search.*,
    com.day.cq.search.result.*"
 
 %>
 
<%@page import="com.day.cq.dam.api.Asset"%>
 
<% 
SlingRepository slingRep = sling.getService(SlingRepository.class); 
Session session = slingRep.loginAdministrative(null);
QueryBuilder qb ; 
Map<String, String> map = new HashMap<String,String>();
map.put("type", "dam:Asset");
map.put("property", "jcr:content/metadata/dc:format"); 
map.put("property.value", "image/jpeg");
qb=resource.getResourceResolver().adaptTo(QueryBuilder.class);
Query query = qb.createQuery(PredicateGroup.create(map), session);
 
SearchResult sr= query.getResult();
String assetPath=null; 
 
 // iterating over the results
  for (Hit hit : sr.getHits()) {
      String path = hit.getPath();
      Resource rs = resourceResolver.getResource(path);
      Asset asset = rs.adaptTo(Asset.class);     
     assetPath = asset.getPath(); 
%>
<img src="<%= assetPath %>" />
<%

}
%>

Avatar

Level 4

Thanks For the support.

We implemented using custom carousel  below code for your reference

<%@page import="com.day.cq.tagging.*,com.day.cq.wcm.api.*"%>

<%@ page import="java.util.*,
    javax.jcr.*,
    org.apache.sling.api.resource.*,
    org.apache.sling.api.scripting.*,
    org.apache.sling.jcr.api.*,
    com.day.cq.search.*,
    com.day.cq.search.result.*,
com.day.cq.dam.api.DamConstants,
com.day.cq.search.result.*"

%>


%@page import="com.day.cq.dam.api.Asset"%



<%
SlingRepository slingRep = sling.getService(SlingRepository.class);
Session session = slingRep.loginAdministrative(null);
QueryBuilder qb ;
Map<String, String> map = new HashMap<String,String>();
map.put("type", "dam.Asset");
map.put("path","/content/dam/emitra/photogallery");
map.put("type", DamConstants.NT_DAM_ASSET);
log.info("NT_DAM_ASSET----------->"+DamConstants.NT_DAM_ASSET);
map.put("property", "jcr:content/metadata/dc:format");
map.put("property.value", "image/jpeg");

qb=resource.getResourceResolver().adaptTo(QueryBuilder.class);
log.info("qb----------->"+qb);
Query query = qb.createQuery(PredicateGroup.create(map), session);
log.info("query----------->"+query);
SearchResult sr= query.getResult();
log.info("sr------->"+sr);
String assetPath="/content/dam/emitra/photogallery";

// iterating over the results
  for (Hit hit : sr.getHits()) {
      String path = hit.getPath();
  log.info("path------->"+path);
      Resource rs = resourceResolver.getResource(path);
      log.info("rs------->"+rs);
      Asset asset = rs.adaptTo(Asset.class);
      log.info("asset----------->"+asset);
      assetPath = asset.getPath();
      log.info("assetPath----------->"+assetPath);
%>

            <div class="col-lg-3 col-md-4 col-xs-6 thumb">


                <a href="<%= asset.getPath()%>" class="thumbnail" target="_blank" title="Emitra Gallery Pic 1" data-gallery><img src="<%= asset.getPath()%>" /></a>


            </div>

<%
}
%>


           
        </div>
        
            </div>
        </div>
    
    </div>