Hi @msgavali111 ,
To get data from the master variation of a content fragment in AEM, you can use the Content Fragment API provided by AEM. Here's an example of how you can retrieve data from the master variation, specifically from the "disclosure" field which contains a list of items:
import com.adobe.cq.dam.cfm.ContentFragment;
import com.adobe.cq.dam.cfm.FragmentData;
import com.adobe.cq.dam.cfm.Variation;
import com.adobe.cq.dam.cfm.VariationTemplate;
import com.adobe.cq.dam.cfm.VariationTemplateManager;
import com.adobe.cq.dam.cfm.VariationTemplateManagerFactory;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
@Component(service = MyComponent.class)
public class MyComponent {
@Reference
private ResourceResolverFactory resourceResolverFactory;
@Reference
private VariationTemplateManagerFactory variationTemplateManagerFactory;
public void getDataFromMasterVariation() {
ResourceResolver resourceResolver = null;
try {
// Get a resource resolver
resourceResolver = resourceResolverFactory.getServiceResourceResolver(null);
// Get the VariationTemplateManager
VariationTemplateManager variationTemplateManager = variationTemplateManagerFactory.getVariationTemplateManager(resourceResolver);
// Get the VariationTemplate for the content fragment
VariationTemplate variationTemplate = variationTemplateManager.getVariationTemplate("/content/dam/myfragment");
// Get the master variation
Variation masterVariation = variationTemplate.getMasterVariation();
// Get the ContentFragment for the master variation
ContentFragment contentFragment = masterVariation.adaptTo(ContentFragment.class);
// Get the data from the "disclosure" field
FragmentData disclosureData = contentFragment.getElement("disclosure");
// Access the individual items in the disclosure field
for (int i = 0; i < disclosureData.size(); i++) {
FragmentData itemData = disclosureData.getElement(i);
String itemValue = itemData.getValue();
// Do something with the item value
}
} catch (Exception e) {
// Handle any exceptions
} finally {
// Close the resource resolver
if (resourceResolver != null) {
resourceResolver.close();
}
}
}
}
In this example, we use the ContentFragment API to retrieve the data from the master variation of a content fragment. We obtain the ContentFragment object for the master variation and then access the "disclosure" field using the getElement() method. The FragmentData object returned by getElement() represents the list of items in the "disclosure" field. We can iterate over the items and retrieve their values using the getValue() method.
Note that the specific implementation may vary depending on your project structure and requirements. Make sure to import the necessary classes and handle any exceptions that may occur during the process.