How to get data from master variation of a content fragment ? | Community
Skip to main content
Level 2
May 19, 2024
Solved

How to get data from master variation of a content fragment ?

  • May 19, 2024
  • 2 replies
  • 848 views

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

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 HrishikeshKagne

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.

2 replies

sarav_prakash
Community Advisor
Community Advisor
May 20, 2024

This is a duplicate of your previous question

https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/how-to-implement-below-use-case-related-to-content-fragments-in/td-p/674038

 

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

https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/content-fragment-get-value-by-name-and-not-node/td-p/393626

If you ran into any specific error, please post the error you get and can further guide. 

HrishikeshKagne
Community Advisor
HrishikeshKagneCommunity AdvisorAccepted solution
Community Advisor
May 20, 2024

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.

Hrishikesh Kagane