Trying to create CF programmatically | Community
Skip to main content
Madhur-Madan
Community Advisor
Community Advisor
February 7, 2023
Solved

Trying to create CF programmatically

  • February 7, 2023
  • 2 replies
  • 758 views

I am trying to create CF programmatically using a servlet. But, I do not see them being generated after servlet completion.

Resource parentRsc = resolver.getResource("<parent-path>");
        Resource templateOrModelRsc = request.getResourceResolver().getResource("<cf-model-path>");
        FragmentTemplate tpl = templateOrModelRsc.adaptTo(FragmentTemplate.class);
        try {
            ContentFragment newFragment = tpl.createFragment(parentRsc, "test", "A fragment description.");
        } catch (ContentFragmentException e) {
            logger.error(e.getMessage(),e);
        }

Any pointers would be appreciated.

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 ShaileshBassi

Hi @madhur-madan 

Is there any error you see in the logs ?

One possibility could be due to the access issue with the request ResourceResolver or it could be due to the commit() which is missing in the code block shared.

 

 

ResourceResolver resolver = getResourceResolver(resolverFactory, "sub-service"); Resource parentRsc = resolver.getResource("<parent-path>"); Resource templateOrModelRsc = resolver.getResource("<cf-model-path>"); FragmentTemplate tpl = templateOrModelRsc.adaptTo(FragmentTemplate.class); try { ContentFragment newFragment = tpl.createFragment(parentRsc, "test", "A fragment description."); resolver.commit(); } catch (ContentFragmentException e) { logger.error(e.getMessage(),e); } Code for getting the system resource resolver public static ResourceResolver getResourceResolver(final ResourceResolverFactory resourceResolverFactory, final String systemUser) throws LoginException { final Map<String, Object> param = new HashMap<>(); param.put(ResourceResolverFactory.SUBSERVICE, systemUser); return resourceResolverFactory.getServiceResourceResolver(param); }

 

 

By using the system resource resolver, the chances of logged in user not have the specific write permissions, goes away. 

 

Hope this helps!

Thanks

2 replies

ShaileshBassi
Community Advisor
ShaileshBassiCommunity AdvisorAccepted solution
Community Advisor
February 7, 2023

Hi @madhur-madan 

Is there any error you see in the logs ?

One possibility could be due to the access issue with the request ResourceResolver or it could be due to the commit() which is missing in the code block shared.

 

 

ResourceResolver resolver = getResourceResolver(resolverFactory, "sub-service"); Resource parentRsc = resolver.getResource("<parent-path>"); Resource templateOrModelRsc = resolver.getResource("<cf-model-path>"); FragmentTemplate tpl = templateOrModelRsc.adaptTo(FragmentTemplate.class); try { ContentFragment newFragment = tpl.createFragment(parentRsc, "test", "A fragment description."); resolver.commit(); } catch (ContentFragmentException e) { logger.error(e.getMessage(),e); } Code for getting the system resource resolver public static ResourceResolver getResourceResolver(final ResourceResolverFactory resourceResolverFactory, final String systemUser) throws LoginException { final Map<String, Object> param = new HashMap<>(); param.put(ResourceResolverFactory.SUBSERVICE, systemUser); return resourceResolverFactory.getServiceResourceResolver(param); }

 

 

By using the system resource resolver, the chances of logged in user not have the specific write permissions, goes away. 

 

Hope this helps!

Thanks

joerghoh
Adobe Employee
Adobe Employee
February 9, 2023

Did you do a resolver.commit(); afterwards?