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

Step by step tutorial on Creating Experience Fragments in AEM

Lets walk you through the process of creating an Experience Fragment and authoring it to a page. Remember Experience Fragments can be authored on third-party systems too, for the demo I am using a test AEM page.

Steps

After logging into AEM, click on ‘Experience Fragments’ , then from create button click on the ‘Experience Fragment’. The folder option allows us to categorise the Experience Fragments.

Once created, start filling out the details in properties section and ‘First Variant’  as shown below.

On the ‘First Variant’ section select the template for the Experience Fragment. HTML fragment which I am going to select is a default provided by AEM.

Now select the created Experience Fragment, and click on edit option.

We will have authoring page opened for the Experience Fragment. We can author assets, components and variations for a fragment.

In assets section, I have authored an image ( We.Retail instore image).

In variations, we can click on ‘create’ to author variations.

Start filling the variations. The template can be any default Experience Fragment template.

Below given various menu options on the Experience Fragment authoring.

Once done with Experience Fragment creation, we will go ahead and author the fragment on a page. For this I have created a test page.
On this page, author an Experience Fragment as a component(‘Drag COmponents here’).

Once this is done we can see the Experience Fragment is rendered on the test page as shown below.

Click me to watch The YouTube demo video