Expand my Community achievements bar.

I need to export content fragments to target through custom workflow

Avatar

Level 1

I have a project where content fragments need to be automatically exported as JSON to a target location upon creation via a custom workflow. The JSON format should match what is generated when accessing the URL: http://localhost:4502/api/assets/contentfragment.model.json.

However, I am facing an issue where I can't replicate the exact JSON format needed for export. Despite my efforts with the code below, the resource resolver is unable to recognise the resource at api/assets.

Resource modelJsonResource = resolver.getResource("http://localhost:4502/api/assets/contentfragment.model.json");
            if (modelJsonResource != null) {
InputStream jsonContent = modelJsonResource.adaptTo(InputStream.class); if (jsonContent != null) {
sendToTarget(jsonContent); } }


Could someone help me achieve the expected JSON output?

1 Reply

Avatar

Level 7

Hi @harshak11490245 ,

 

Your resource resolver can't recognize URL. You should find content fragment by path e.g. /content/dam/wknd-shared/en/adventures/napa-wine-tasting/napa-wine-tasting, adapt to ContentFragment model and export it with Jackson exporter.

 

Below you can find a code an example of OSGi component how to get Content Fragment in JSON programmatically:

@Component
public class CfConverter {
   @Reference
   private org.apache.sling.models.factory.ModelFactory modelFactory;

   public String exportCfInJSON(ResourceResolver resourceResolver, String cfPath) {
       Resource cfResource = resourceResolver.getResource(cfPath);
       if (cfResource == null) {
           return null;
       }
       ContentFragment model = cfResource.adaptTo(com.adobe.cq.dam.cfm.ContentFragment.class)
       if (model == null) {
           return null;
       }
       return modelFactory.exportModel(model, 'jackson', String.class, Map.of());
   }
}

 

Best regards,

Kostiantyn Diachenko.