Hi,
I'm looking for a way for our librarians to export metadata of assets based on search results. For example, I searched with "jet" and I got 1000 assets as a result that spans across multiple folders. I think there is no OOB way to get this. Can anyone let me know if there is a way to achieve this?
Thanks,
Rahul
Solved! Go to Solution.
Views
Replies
Total Likes
Hi @RahulMohan
To achieve your goal of exporting asset metadata based on search results in AEM, you'll primarily use the AEM QueryBuilder API in conjunction with the Sling API.
since i dont know how you want to utilize this in your project context i am providing you a demo servlet code on how you can achieve that.
import org.apache.sling.api.resource.*;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.query.api.Query;
import org.apache.sling.query.api.QueryImpl;
import com.fasterxml.jackson.databind.ObjectMapper;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MetadataExportServlet extends SlingAllMethodsServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try (ResourceResolver resourceResolver = request.getResourceResolver()) {
String dcTitle = request.getParameter("dc:title");
QueryBuilder builder = resourceResolver.adaptTo(QueryBuilder.class);
Query query = builder.from("/content/dam")
.where("jcr:contains", dcTitle)
.and("jcr:primaryType", "dam:Asset");
List<Resource> searchResults = query.find();
List<Map<String, Object>> assetMetadataList = new ArrayList<>();
for (Resource assetResource : searchResults) {
ValueMap properties = assetResource.getValueMap();
Map<String, Object> assetMetadata = new HashMap<>();
assetMetadata.put("title", properties.get("jcr:title", String.class));
// Add other metadata properties as needed
assetMetadataList.add(assetMetadata);
}
ObjectMapper objectMapper = new ObjectMapper();
String jsonOutput = objectMapper.writeValueAsString(assetMetadataList);
response.setContentType("application/json");
PrintWriter writer = response.getWriter();
writer.write(jsonOutput);
writer.flush();
} catch (Exception e) {
// Handle exceptions with proper logging and error messages
}
}
}
obviously you can modify this code based on your requirements, this is one way how you can get the metadata properties as json.
attaching a similar blog for your reference:
https://www.tothenew.com/blog/custom-reporting-in-aem-digital-asset-management-dam/
Hi @RahulMohan
To achieve your goal of exporting asset metadata based on search results in AEM, you'll primarily use the AEM QueryBuilder API in conjunction with the Sling API.
since i dont know how you want to utilize this in your project context i am providing you a demo servlet code on how you can achieve that.
import org.apache.sling.api.resource.*;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.query.api.Query;
import org.apache.sling.query.api.QueryImpl;
import com.fasterxml.jackson.databind.ObjectMapper;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MetadataExportServlet extends SlingAllMethodsServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try (ResourceResolver resourceResolver = request.getResourceResolver()) {
String dcTitle = request.getParameter("dc:title");
QueryBuilder builder = resourceResolver.adaptTo(QueryBuilder.class);
Query query = builder.from("/content/dam")
.where("jcr:contains", dcTitle)
.and("jcr:primaryType", "dam:Asset");
List<Resource> searchResults = query.find();
List<Map<String, Object>> assetMetadataList = new ArrayList<>();
for (Resource assetResource : searchResults) {
ValueMap properties = assetResource.getValueMap();
Map<String, Object> assetMetadata = new HashMap<>();
assetMetadata.put("title", properties.get("jcr:title", String.class));
// Add other metadata properties as needed
assetMetadataList.add(assetMetadata);
}
ObjectMapper objectMapper = new ObjectMapper();
String jsonOutput = objectMapper.writeValueAsString(assetMetadataList);
response.setContentType("application/json");
PrintWriter writer = response.getWriter();
writer.write(jsonOutput);
writer.flush();
} catch (Exception e) {
// Handle exceptions with proper logging and error messages
}
}
}
obviously you can modify this code based on your requirements, this is one way how you can get the metadata properties as json.
attaching a similar blog for your reference:
https://www.tothenew.com/blog/custom-reporting-in-aem-digital-asset-management-dam/
@RahulMohan If you dont want to write code , just run the below query in query builder
path=/content/dam/<PATH>
p.hits=full
p.limit=-1
p.nodedepth=3
fulltext=jet
You will see the "JSON QueryBuilder Link " in the bottom and it gives the complete json with metadata of the searched assets , you can use the any json to csv convertors to convert it to csv file.
Views
Likes
Replies