Create Content Fragment Programmatically

Creating a content fragment programmatically requires setting up a system user with Read permission for the content fragment model and Read, Modify, Create permissions for the asset folder where the content fragment will be stored. Additionally, ensure the content fragment model and asset folder are created for saving the content fragment.
//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 =; String name = element.getName(); FragmentData fragmentData = element.getValue(); fragmentData.setValue(properties.get(name)); element.setValue(fragmentData); }

Frequently Asked Questions (FAQ)

No exceptions but content fragment wasn't created at the specified path.

Ensure to use to save the changes in the repository.