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.

ContentFragmentManager’.

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

//reference the Content Fragment Manager
@Reference
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();

while(variations.hasNext()){
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

One thought on “Create & Access the content fragment programmatically

Leave a comment