Hi all
I am trying to create a content fragment programatically and I am not seeing the fragment being created in my node structure. Below is the code I compiled from different sources over the internet as I couldn't find a proper working example anywhere .
public void createContentFragment(final Resource parentRsc) {
try (ResourceResolver resourceResolver = resourceService.getResourceResolver(CommonConstants.SERVICE_USER)) {
Resource templateOrModelRsc = resourceResolver.getResource("/conf/demo/settings/dam/cfm/models/faq");
if (Objects.nonNull(templateOrModelRsc)) {
FragmentTemplate tpl = templateOrModelRsc.adaptTo(FragmentTemplate.class);
if (Objects.nonNull(tpl)) {
ContentFragment contentFragment = tpl.createFragment(parentRsc, "test", "A fragment description.");
if (null != contentFragment) {
Iterator<ContentElement> elements = contentFragment.getElements();
while (elements.hasNext()) {
ContentElement contentElement = elements.next();
contentElement.setContent("value", contentElement.getContentType());
}
}
Session session = resourceResolver.adaptTo(Session.class);
if (Objects.nonNull(session)) {
session.save();
}
}
}
} catch (Exception e) {
LOGGER.error("Exception ", e);
}
}
Update :- Thanks @shubhanshu_singh for the correct answer. Anyone facing a similar issue , please also refer my response https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/create-a-content-fragment-... , to understand what went wrong in my case.
Thanks
Veena
Solved! Go to Solution.
Hi @VeenaVikraman , Below code seems to be working fine for me kindly check this
@VeenaVikraman Code constant states "CommonConstants.READ_SERVICE_USER", does this service user has create permission under that parentRsc node?
Thanks @Saravanan_Dharmaraj My bad. I frgot to rename that service user since I was trying hard get this working. Yes that user has write perimissions for now. Also , I made sure that I am getting the CF objects after creation. But after saving , I should see the CF node in the repository under the given path right ? I am not seeing that . I am not sure if I am doing something wrong in the commit part or is there something more expected to be done before I save the changes.
I have updated the original question.
Hi @VeenaVikraman , Below code seems to be working fine for me kindly check this
Super awesome @shubhanshu_singh . Your answer is perfectly correct. And the culprit in my case was the resolver I was using for commit. I tried commiting the changes like below.
1. Using ResourceResolver I got for my system user
try (ResourceResolver resourceResolver =
resourceService.getResourceResolver(CommonConstants.SERVICE_USER)) {
.........
resourceResolver.commit();
........
}
2. Using Session I got from the Resource Resolver
try (ResourceResolver resourceResolver
= resourceService.getResourceResolver(CommonConstants.SERVICE_USER)) {
........
Session session = resourceResolver.adaptTo(Session.class);
LOGGER.info("TPL is {}", session);
session.save();
......
}
But both didn't work. The one that works is the one which @shubhanshu_singh mentioned above
Get the resourceResolver from the contentFragment's Resource and use it to commit.
Resource fragRsc = contentFragment.adaptTo(Resource.class);
if (Objects.nonNull(fragRsc)) {
ResourceResolver resolver = fragRsc.getResourceResolver();
resolver.commit();
}
And this only one changed actually helped