Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

How to create mixed media sets programmatically in aem?

Avatar

Employee

Hi,

We have a requirement in our project to create the mixed media set programmatically for the files having similar file names of 13 digits. There' already imagesets create automatically. Similar way we need to try for mixed media set if there's images and videos having same 13 digits add it to the mixed media set. Is it possible to create mixedmedia sets programmatically?

Keerthana_H_N_0-1690527557323.png

 

1 Accepted Solution

Avatar

Correct answer by
Employee

We have found alternate API which is more suitable for automating mixed media set and it's working as expected. Below are the details:

SetHelper class has createSet api to create the set resource
and then updateSet api to add members to it as well as generate thumbnails these are all the set types we can pass in it

            S7damConstants.S7_IMAGE_SET,
            S7damConstants.S7_SPIN_SET,
            S7damConstants.S7_SWATCH_SET,
            S7damConstants.S7_MIXED_MEDIA_SET,
            S7damConstants.S7_VIDEO_SET,
            S7damConstants.S7_CAROUSEL_SET

it supports thumbnail generation as well 

Implementing using this method is creating the mixedmediaset automatically and adding the members from imageset, spinset and video.

 

Thanks,

Priyanka

View solution in original post

12 Replies

Avatar

Employee

Hi @Keerthana_H_N , 

You can try using the Java Content Repository (JCR) API for creating mixed media sets programmatically. JCR API allows you to perform CRUD operations programmatically so using that you can create the nodes corresponding to the mixed media sets programmatically. 


Please refer to the below code as a sample : 

import javax.jcr.Node;
import javax.jcr.Repository;
import javax.jcr.Session;
import javax.jcr.SimpleCredentials;

import org.apache.jackrabbit.commons.JcrUtils;

public class MixedMediaSetCreator {

    public static void main(String[] args) {
        String aemInstanceUrl = "http://localhost:4502"; // Replace with your AEM instance URL
        String username = "admin"; // Replace with a valid AEM username
        String password = "admin"; // Replace with the password for the AEM user

        createMixedMediaSet(aemInstanceUrl, username, password);
    }

    public static void createMixedMediaSet(String aemInstanceUrl, String username, String password) {
        Session session = null;
        try {
            // Establish a connection to the AEM repository
            Repository repository = JcrUtils.getRepository(aemInstanceUrl);
            session = repository.login(new SimpleCredentials(username, password.toCharArray()));

            // Specify the path where you want to create the mixed media set
            String mixedMediaSetPath = "/content/mixed-media-sets/my-mixed-media-set";

            // Create the node for the mixed media set
            Node mixedMediaSetNode = session.getNode(mixedMediaSetPath);

            // Set properties for the mixed media set
            mixedMediaSetNode.setProperty("jcr:title", "My Mixed Media Set");
            mixedMediaSetNode.setProperty("jcr:description", "This is a sample mixed media set.");
            mixedMediaSetNode.setProperty("tags", new String[] {"tag1", "tag2"});

            // Add references to assets in the mixed media set
            mixedMediaSetNode.setProperty("assets", new String[] {
                    "/content/dam/assets/image1.jpg",
                    "/content/dam/assets/image2.jpg",
                    "/content/dam/assets/video1.mp4"
            });

            // Save the changes
            session.save();

            System.out.println("Mixed media set created successfully!");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null) {
                session.logout();
            }
        }
    }
}


For getting complete details about the JCR API and its capabilities you can refer to the following link : https://experienceleague.adobe.com/docs/experience-manager-65/developing/platform/access-jcr.html?la... 

Avatar

Level 3

Hi @ayushmishra07 , thank you for sharing the details. We have tried using the above implementation in our logic and facing issue while calling getRepository method. 

Repository repository = JcrUtils.getRepository(aemInstanceUrl);

will it work for any AEM version ? Currently we are using AEMaaCS jar file in our local.

 

 

Avatar

Employee

I haven't tried it on AEMaaCS, but it is not present in the list of deprecated APIs, so I believe it should be accessible.
Can you please confirm weather you added jackrabbit-standalone-2.4.0.jar file to your Java application’s class path or not. Also, can you please share the error which you received. 

Avatar

Level 3

Hi,

 

Facing similar issue while connecting from external java application? do we have solution for this?

Avatar

Employee

@Keerthana_H_N  yes its possible.

you can use com.day.cq.dam.commons.util.S7SetHelper class.

 and then you can use as below

set = S7SetHelper.createS7MixedMediaSet(res, name, props);

 

Avatar

Level 3

Hi @Nishant-Singh , I am observing after creating mixed media set programmatically using S7SetHelper class -  S7SetHelper.createS7MixedMediaSet(newResource, imageSetData, props); 

Sets are having processing label all the time. Not completing the processing. Also in the props how can we add properties similar to how it will add while using OOTB. For example in ootb we have s7Set node under related node and sling:members with all the list of assets, also few properties have to be added to metadata node as well.

thanikon_0-1690804751869.png

 

Thanks.

 

Avatar

Employee

you need to adapt image as Asset

Asset asset1 = resourceAsset1.adaptTo(Asset.class);
Asset asset2 = resourceAsset2.adaptTo(Asset.class);

then you need to add those to Mixed media set.

MediaSet mixedMediaSet = S7SetHelper.createS7MixedMediaSet(newResource, imageSetData, props); 

 

mixedMediaSet.add(asset1);

mixedMediaSet.add(asset1);

Avatar

Level 3

Got it @Nishant-Singh .Thanks a lot.  Also we are creating mixed media sets programmatically for video file and image set. When we are adding assets to mixed media sets we need to add references from image set. All sling:members under image set need to be added to mixed media set. Any suggestion in extracting the image set references and add it to newly created mixed media set. Thanks 

Avatar

Employee

@Nishant-Singh We are able to create mixed media set programmatically and node hierarchy is also same as out of box functionality  but rendition folder is by default empty.

Do you have any idea how can we generate renditions for mixed media set which will create thumbnail.

Programmatically created node using  S7SetHelper.createS7MixedMediaSet

PriyankaKu_0-1691393476864.png

Out of box created nodes:

PriyankaKu_1-1691393522554.png

 

Thanks,

Avatar

Employee

try using below 2 properties and then check.

 

            props.put("dam:imageServerAsset", true);
            props.put("dam:assetState", "processing");

 

Avatar

Correct answer by
Employee

We have found alternate API which is more suitable for automating mixed media set and it's working as expected. Below are the details:

SetHelper class has createSet api to create the set resource
and then updateSet api to add members to it as well as generate thumbnails these are all the set types we can pass in it

            S7damConstants.S7_IMAGE_SET,
            S7damConstants.S7_SPIN_SET,
            S7damConstants.S7_SWATCH_SET,
            S7damConstants.S7_MIXED_MEDIA_SET,
            S7damConstants.S7_VIDEO_SET,
            S7damConstants.S7_CAROUSEL_SET

it supports thumbnail generation as well 

Implementing using this method is creating the mixedmediaset automatically and adding the members from imageset, spinset and video.

 

Thanks,

Priyanka