Add assets to collections programatically | Community
Skip to main content
June 18, 2024
Solved

Add assets to collections programatically

  • June 18, 2024
  • 2 replies
  • 915 views

Hi Team,

How to  add assets and asset folders to collections(/content/dam/collections) programmatically by servlet?

Kindly suggest. Thanks.

 

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 lukasz-m

Hi @aemcomm,

There are no public available collections java api that you could use, apart of the one pointed by @saravanan_dharmaraj which supports lightbox only.

However you can use ResourceCollectionManager and ResourceCollection from Sling - this api is used by internal AEM mechanisms to support collection.

It can be used like below:

// place for imports import org.apache.sling.api.servlets.SlingAllMethodsServlet; import org.apache.sling.resource.collection.ResourceCollection; import org.apache.sling.resource.collection.ResourceCollectionManager; public class CollectionServlet extends SlingAllMethodsServlet { @Reference private ResourceCollectionManager resourceCollectionManager; @Override protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException { // path to collection String collectionPath = "/content/dam/collections/L/L7vWaG-HBgET5udyUwdI/test-collection"; // path to asset String assetPath = "/content/dam/we-retail/en/activities/biking/forest-trail.jpg"; ResourceResolver resourceResolver = request.getResourceResolver(); Resource collection = resourceResolver.getResource(collectionPath); Resource asset = resourceResolver.getResource(assetPath); if (collection != null && asset != null) { ResourceCollection rc = resourceCollectionManager.getCollection(collection); if (rc != null) { if (rc.add(asset)) { resourceResolver.commit(); } } } } }

You can pass asset and collection path as a parameter, above example used hardcoded values for simplification.

2 replies

Saravanan_Dharmaraj
Community Advisor
Community Advisor
June 18, 2024

@aemcomm You can use lightbox collection API 

https://javadoc.io/static/com.adobe.aem/uber-jar/6.5.21/com/day/cq/dam/api/lightbox/Lightbox.html

add(Asset asset)

method should do. 

please check it out. 

lukasz-m
Community Advisor
lukasz-mCommunity AdvisorAccepted solution
Community Advisor
June 19, 2024

Hi @aemcomm,

There are no public available collections java api that you could use, apart of the one pointed by @saravanan_dharmaraj which supports lightbox only.

However you can use ResourceCollectionManager and ResourceCollection from Sling - this api is used by internal AEM mechanisms to support collection.

It can be used like below:

// place for imports import org.apache.sling.api.servlets.SlingAllMethodsServlet; import org.apache.sling.resource.collection.ResourceCollection; import org.apache.sling.resource.collection.ResourceCollectionManager; public class CollectionServlet extends SlingAllMethodsServlet { @Reference private ResourceCollectionManager resourceCollectionManager; @Override protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException { // path to collection String collectionPath = "/content/dam/collections/L/L7vWaG-HBgET5udyUwdI/test-collection"; // path to asset String assetPath = "/content/dam/we-retail/en/activities/biking/forest-trail.jpg"; ResourceResolver resourceResolver = request.getResourceResolver(); Resource collection = resourceResolver.getResource(collectionPath); Resource asset = resourceResolver.getResource(assetPath); if (collection != null && asset != null) { ResourceCollection rc = resourceCollectionManager.getCollection(collection); if (rc != null) { if (rc.add(asset)) { resourceResolver.commit(); } } } } }

You can pass asset and collection path as a parameter, above example used hardcoded values for simplification.

AEMCommAuthor
June 19, 2024

Thank you @lukasz-m it worked. 

Could you also please help me to know to is there any specific API to create a project under /content/dam/project?

Node API works as expected but I'm still searching for relevant API to create a project. Kindly help. 

Thank you