AEM 6.5.5 - Not able to create content fragments programmatically | Community
Skip to main content
nikunjj81682294
Level 2
August 15, 2020
Solved

AEM 6.5.5 - Not able to create content fragments programmatically

  • August 15, 2020
  • 1 reply
  • 4472 views

I am trying to run the following code to create content fragments programmatically. The code runs fine but there is no content fragment created in the DAM.

 

I am able to create the content fragments manually but not programmatically.

 

//Does not create a content fragment
Resource templateRes = resourceResolver.getResource("/conf/<projectname>/settings/dam/cfm/models/custom-model");
fragment = templateRes.adaptTo(FragmentTemplate.class).createFragment(parentFolder, "custom-name", "Custom Title");

// Asset gets created successfully.
AssetManager assetMgr = resourceResolver.adaptTo(AssetManager.class);
Resource resource = resourceResolver.getResource(parentFolder.getPath()+"/"+"test.jpeg");
Asset asset = assetMgr.createAsset(parentFolder.getPath()+"/"+"test.jpeg", null, "image/jpeg", true);

resourceResolver.commit();

 Any help is appreciated.

 

Thanks,

Nikunj Jariwala

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by vanegi

To create a content fragment, you can use ‘create’ API reference from ‘com.adobe.cq.dam.cfm ContentFragmentManager’.

 

Sharing a sample code which you can try to create a content fragment programmatically: 

//reference the Content Fragment Manager
@3214626
private ContentFragmentManager fragmentManager;

private void createFrag() {
/** 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
}

 

Reference: https://helpx.adobe.com/experience-manager/6-5/sites/developing/using/reference-materials/javadoc/com/adobe/cq/dam/cfm/ContentFragmentManager.html

 

Thanks!!

1 reply

vanegi
Adobe Employee
vanegiAdobe EmployeeAccepted solution
Adobe Employee
August 16, 2020

To create a content fragment, you can use ‘create’ API reference from ‘com.adobe.cq.dam.cfm ContentFragmentManager’.

 

Sharing a sample code which you can try to create a content fragment programmatically: 

//reference the Content Fragment Manager
@3214626
private ContentFragmentManager fragmentManager;

private void createFrag() {
/** 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
}

 

Reference: https://helpx.adobe.com/experience-manager/6-5/sites/developing/using/reference-materials/javadoc/com/adobe/cq/dam/cfm/ContentFragmentManager.html

 

Thanks!!

January 8, 2021

Hi @vanegi,

The solution you have mentioned is marked as deprecated, If you could provide sample code for creation of content fragment in new pattern, that will be really helpful.

 

Thanks,

Vara Prasad M

Kiran_Vedantam
Community Advisor
Community Advisor
January 9, 2023

Hi @varaprasad56 

 

Not sure if you got the solution, but, here is the code snippet which worked for me:

 

Resource templateOrModelRsc = resourceResolver.getResource("...");
FragmentTemplate tpl = templateOrModelRsc.adaptTo(FragmentTemplate.class);
ContentFragment newFragment = tpl.createFragment(parentRsc, "A fragment name", "A fragment description.");

resourceResolver.commit();

 

Thanks,

Kiran Vedantam