Programmatically Create Content fragments and variations with the help of ContentFragment API | Community
Skip to main content
eswarvarun
November 24, 2022
Solved

Programmatically Create Content fragments and variations with the help of ContentFragment API

  • November 24, 2022
  • 3 replies
  • 3708 views

The requirement is create the content fragment programmatically and update the master and child variants data dynamically while iteration.

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 Love_Sharma

I did similar thing in my previous engagement where I migrated content from legacy CMS to AEM. I used Content fragment APIs to do that. You can explore it more here 

https://javadoc.io/static/com.adobe.aem/aem-sdk-api/2020.6.3696.20200610T021539Z-200604/com/adobe/cq/dam/cfm/FragmentTemplate.html

Code will look like

import com.adobe.cq.dam.cfm.* - You will find everything you need inside this package
FragmentTemplate fragmentTemplate = templateResource.adaptTo(FragmentTemplate.class); ContentFragment contentFragment = fragmentTemplate.createFragment(Resource res, cfName, cfTitle);

 Once your template is created you can start populating it.

 

And try searching the community, it has many similar requests. One of them is 

https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/how-can-we-create-content-fragment-via-code/td-p/425582

 

3 replies

Mohit_KBansal
Adobe Employee
Adobe Employee
November 24, 2022

You should use Asset APIs [1] to create content fragments and their variations. 

 

[1] https://experienceleague.adobe.com/docs/experience-manager-65/assets/extending/assets-api-content-fragments.html?lang=en 

Community Advisor
November 24, 2022

Hi,

There are multiple options to programmatically create Content Fragments in AEM.

1. You can use Assets HTTP API to create content fragments once you know what needs to go into the Fragment:

https://docs.adobe.com/content/help/en/experience-manager-65/assets/extending/assets-api-content-fra...

2. If you were already within AEM's context, you could also create content fragments programmatically as described in the following link: https://docs.adobe.com/content/help/en/experience-manager-65/developing/extending-aem/customizing-co...

3.Sample code - https://aemdeveloper.wordpress.com/2017/08/22/create-access-the-content-fragment-programmatically/

 

Thanks

Love_Sharma
Love_SharmaAccepted solution
Level 4
November 24, 2022

I did similar thing in my previous engagement where I migrated content from legacy CMS to AEM. I used Content fragment APIs to do that. You can explore it more here 

https://javadoc.io/static/com.adobe.aem/aem-sdk-api/2020.6.3696.20200610T021539Z-200604/com/adobe/cq/dam/cfm/FragmentTemplate.html

Code will look like

import com.adobe.cq.dam.cfm.* - You will find everything you need inside this package
FragmentTemplate fragmentTemplate = templateResource.adaptTo(FragmentTemplate.class); ContentFragment contentFragment = fragmentTemplate.createFragment(Resource res, cfName, cfTitle);

 Once your template is created you can start populating it.

 

And try searching the community, it has many similar requests. One of them is 

https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/how-can-we-create-content-fragment-via-code/td-p/425582

 

eswarvarun
December 30, 2022

I have created the content fragment and content fragment variants programmatically based on the data present in the map.

 

public static void createVariations(ResourceResolver resourceResolver, String variationTitle,
Map<String, Object> variationData, String fragmentTitle, String pagePath, SlingHttpServletResponse response)
throws ContentFragmentException, IOException, ParseException {
Resource fragmentResource = resourceResolver
.getResource(CFM_ROOT_PATH + "/" + lowerCaseConverter(fragmentTitle));
if (null != fragmentResource) {
ContentFragment cfm = fragmentResource.adaptTo(ContentFragment.class);
Iterator<VariationDef> variations = cfm.listAllVariations();
boolean variationExists = false;
while (variations.hasNext()) {
VariationDef variation = variations.next(); //
if (variation.getName().equals(titleFormater(variationTitle))) {
variationExists = true;
}
}
if (!variationExists) {
// create the variation
cfm.createVariation(titleFormater(variationTitle), variationTitle, "");
}
Iterator<ContentElement> itr = cfm.getElements();
while (itr.hasNext()) {
ContentElement cfElement = itr.next();
ContentVariation cv = cfElement.getVariation(titleFormater(variationTitle));
FragmentData fragmentData = cv.getValue();
if (cfElement.getName().equals("title")) {
fragmentData.setValue(variationData.get("title"));
}
if (cfElement.getName().equals("href")) {
fragmentData.setValue(variationData.get("path"));
}
if (cfElement.getName().equals("softwareVersion")) {
fragmentData.setValue(variationTitle);
}
if (cfElement.getName().equals("releaseNotes")) {
fragmentData.setValue(variationData.get("notesPath"));
}
if (cfElement.getName().equals("downloadSizeUom")) {
fragmentData.setValue(variationData.get("sizeUnit"));
}
if (cfElement.getName().equals("downloadSize")) {
fragmentData.setValue(variationData.get("size"));
}
if (cfElement.getName().equals("releaseDate")) {
if (Objects.nonNull(variationData.get("releaseDate"))) {
fragmentData.setValue(variationData.get("releaseDate"));
}
}
if (cfElement.getName().equals("archived")) {
fragmentData.setValue(variationData.get("archivedSoftware"));
}
if (cfElement.getName().equals("category")) {
Resource cfResource = cfm.adaptTo(Resource.class);
if (Objects.nonNull(cfResource)) {
Resource variationResource = cfResource
.getChild(JcrConstants.JCR_CONTENT + "/data/" + titleFormater(variationTitle));
if (Objects.nonNull(variationResource)) {
Node variationNode = variationResource.adaptTo(Node.class);
if (Objects.nonNull(variationNode)) {
try {
//Updating the Tags
Set<String> categoryList = new HashSet<>();
if (variationNode.hasProperty("category")) {
Value[] categories = variationNode.getProperty("category").getValues();
for (Value value : categories) {
categoryList.add(value.getString());
}
}
categoryList.add(tagGenerator(variationData.get("pageTags").toString()));
fragmentData.setValue(categoryList.toArray());
} catch (ValueFormatException e) {
log.error("ValueFormatException error {}.", e.getMessage(), e);
} catch (PathNotFoundException e) {
log.error("PathNotFoundException error {}.", e.getMessage(), e);
} catch (RepositoryException e) {
log.error("RepositoryException error {}.", e.getMessage(), e);
}

}

}

}

}
cv.setValue(fragmentData);
}
}
resourceResolver.commit();
}