Expand my Community achievements bar.

Unable to update field in content fragment

Avatar

Level 3

Hi,

I have a requirement to create and update a value in content fragment. I am able to create the content fragment but when i am trying to save the field it is not happening.

        try {            
            Map<String,Object> param = new HashMap<>();
            param.put(ResourceResolverFactory.SUBSERVICE,CF_SERVICE_USER);
            resourceResolver = resolverFactory.getServiceResourceResolver(param);
            Resource templateResource = resourceResolver.getResource("model-path");
            Resource parentResource = resourceResolver.getResource("folder-path");
            FragmentTemplate template = templateResource.adaptTo(FragmentTemplate.class);
            ContentFragment newFragment = template.createFragment(parentResource, "TestCF", "Content Fragment New field test");
            Resource newFragmentRsc=resourceResolver.getResource("/content/dam/cf/TestCF");
            newFragmentRsc.adaptTo(ValueMap.class).put("dateField","2024-10-13T13:48:00.000+05:30");           
            resourceResolver.commit();
        }  
        return "CF Created";
    }
4 Replies

Avatar

Adobe Champion

in your example you are creating the fragment, then loading the root resource which was created and setting values on this - CF's have a much more complex internal structure as the data is not stored directly on the root resource:

 

Screenshot 2024-10-15 at 10.15.41.png

 

You are getting a ContentFragment object back from the createFragment call, so should be able to use the java API to get the relevant element (getElement method) then set the value on it, eg:

 

contentFragment newFragment = template.createFragment(parentResource, "TestCF", "Content Fragment New field test");
ContentElement dateElement = newFragment.getElement("dateField");
dateElement.setContent("2024-10-13T13:48:00.000+05:30",dateElement.getContentType());
resourceResolver.commit();

 

Javadocs for the interfaces:

https://developer.adobe.com/experience-manager/reference-materials/6-5/javadoc/com/adobe/cq/dam/cfm/...

https://developer.adobe.com/experience-manager/reference-materials/6-5/javadoc/com/adobe/cq/dam/cfm/...

 

Avatar

Level 3

Hi @martin_ecx_io 

 

I already tried this method but the value is stored as below:

nivethaS_0-1729058313011.png

 

I was unable to update the datefield still

Avatar

Level 4

Hmm... interesting. Apparently it seems that you need either a ContentElement or a ElementTemplate to add a new element to the existing content fragment, but they are adaptable interfaces, so can't make a direct object out of it. You can still play around using the Resource API or the Node API and a property to the content fragment, but it wont show up in the UI when you edit that fragment and you cannot extract it out by the OOTB methods.

 

I tried the below via my servlet after declaring those fields in the CF model, the String type went through but not the Date type. It simply didn't get persisted.

contentFragment.getElement("testDate").setContent(String.valueOf(Calendar.getInstance()), "Date");
contentFragment.getElement("sampleData").setContent("Going away", "String");

 Sadly, the setContent() method only accepts String values. 

 

I'm interested to know the answer for this one.

 

Regards,

Ramkumar

Avatar

Administrator

@nivethaS Did you find the suggestions helpful? Please let us know if you require more information. Otherwise, please mark the answer as correct for posterity. If you've discovered a solution yourself, we would appreciate it if you could share it with the community. Thank you!



Kautuk Sahni