Expand my Community achievements bar.

Radically easy to access on brand approved content for distribution and omnichannel performant delivery. AEM Assets Content Hub and Dynamic Media with OpenAPI capabilities is now GA.
SOLVED

Add assets to collections programatically

Avatar

Level 1

Hi Team,

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

Kindly suggest. Thanks.

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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.

View solution in original post

3 Replies

Avatar

Correct answer by
Community Advisor

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.

Avatar

Level 1

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