I want to get the data inside master node, and the field which i want to get the data inside disclosure, a list of items. and can we have the data as an composite multifield. like the data should get store in item0, item1, item3 format
Solved! Go to Solution.
Views
Replies
Total Likes
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.
This is a duplicate of your previous question
Please dont raise duplicate question, and preferably attempt few steps by reading documentation and reach out when you run into specific issues. Your exact question was raised by someone in this question
If you ran into any specific error, please post the error you get and can further guide.
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.
Views
Likes
Replies
Views
Likes
Replies