Tuesday 27 June 2017

Create & Access the content fragment programmatically

Content Fragment helps to create content without referring a page. This fragments can be used to showcase the content across various channels.
You can read multiple blogs on the same here.
AEM Leading to head less CMS?
AEM Content Fragment output as JSON
AEM 6.3 Content Fragments Basics
How to create a Content Fragment? step by step tutorial
Create & Access the content fragment programmatically

Programmatic creation, access, modification of Content Fragment

To create a content fragment, we need 'create' API reference from 'com.adobe.cq.dam.cfm.

Once we have the import, use below code to create a content fragment programmatizally.

//reference the Content Fragment Manager
private ContentFragmentManager fragmentManager;

private void myCreateFun() {
    /** fragmentManager.create helps to create a content fragment
        parent - The location where the content fragment should be created (for eg. "/content/dam/fragments")
        template - the content fragment template to refer while creating the new fragment
        my-test-fragment - name of the fragment
        My Test Fragment - title of the fragment **/
    ContentFragment myFragment = fragmentManager.create(parent, template, "my-test-fragment", "My Test Fragment");

Programmatically accessing a content fragment

We need 'com.adobe.cq.dam.cfm.ContentFragment' API reference to access a content fragment

//Get the resource of content fragment as below.
Resource fragmentResource = resourceResolver.getResource("/content/dam/fragments/my-test-fragment");

//Adapt it to a fragment resource
if (fragmentResource != null) {
    ContentFragment fragment = fragmentResource.adaptTo(ContentFragment.class);
    // the resource is now accessible through the API

Programmatically accessing elements from Content Fragment

Iterator<ContentElement> elements = fragment.getElements();
while (elements.hasNext()){
ContentElement element = elements.next();
//your action on element

Programmatically Accessing Content Fragment metadata:

Map<String, Object> getMetaData();

Programmatically Accessing Content Fragment variations:

Iterator<ContentVariation> variations = element.getVariations();

ContentVariations variation = variations.next();
//do the variation process here

Programmatically Accessing  Content Fragment elements/variations by its name

ContentElement title = fragment.getElement("title");
ContentVariation mobileAppVariation = title.getVariation("mobile-app");

Programmatically Accessing Content:

String content = element.getContent();
String contentType = element.getContentType();

Programmatically Modifying Content Fragment content
element.setContent("Content", "text/plain")

Programmatically Modifying Content Fragment metadata
void setMetaData(String name, Object value) throws ContentFragmetException

YouTube demo videos for Content Fragments:
         AEM 6.3 Content Fragments Basics
         Content Fragments AEM
         View Content fragment output in aem


  1. I found this blog very useful. I have one doubt related to CF, how we can resolve the OOTB template used for Content fragment creation into the resource.I am trying resolve the default simple fragment placed at this location /libs/settings/dam/cfm/templates/simple into resource, but ending up with FragmentWriteException: Provided template is invalid. Will you please suggest me the whether I am resolving the correct path for template.

  2. Same as above.Also, I am trying use a content model fragment for structured content, technically that does not have a template. Please let me know if there is a different API

  3. hey you need to give path till jcr:content ie., libs/settings/dam/cfm/templates/simple/jcr:content

  4. Hi Pankaj as to retrieve the template use:


    As you may know the CF were released from 6.3.3 version so you must also use the proper version of the cfm dependency

    also fragmentManager.create has been deprecated instead use