Create Content Fragment Programmatically
Published
Viewed210 times
//Get Resource Resolver using System User
ResourceResolver resolver = resolverService.getResourceResolver();
Resource parentRsc = resolver.getResource(assetPath);
Resource cfmResource = resolver.getResource(cfmPath);
FragmentTemplate template = cfmResource.adaptTo(FragmentTemplate.class);
// Create Content Fragment
String name = JcrUtil.createValidName(title);
ContentFragment fragment = template.createFragment(parentRsc, name, title);
final Iterator<ContentElement> elementIterator = fragment.getElements();
// Update Content Fragment Element Values
while (elementIterator.hasNext()) {
final ContentElement element = elementIterator.next();
String name = element.getName();
FragmentData fragmentData = element.getValue();
fragmentData.setValue(properties.get(name));
element.setValue(fragmentData);
}