The requirement is create the content fragment programmatically and update the master and child variants data dynamically while iteration.
Solved! Go to Solution.
Views
Replies
Total Likes
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
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
You should use Asset APIs [1] to create content fragments and their variations.
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:
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
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
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
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();
}
Views
Likes
Replies