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

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

Avatar

Level 1

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

1 Accepted Solution

Avatar

Correct answer by
Level 5

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...

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-...

 

View solution in original post

4 Replies

Avatar

Community Advisor

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

Avatar

Correct answer by
Level 5

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...

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-...

 

Avatar

Level 1

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();
}