Unable to update field in content fragment | Adobe Higher Education
Skip to main content
Level 3
October 15, 2024

Unable to update field in content fragment

  • October 15, 2024
  • 3 답변들
  • 814 조회

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";
    }
이 주제는 답변이 닫혔습니다.

3 답변

martin_ecx_io
Community Advisor and Adobe Champion
Community Advisor and Adobe Champion
October 15, 2024

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:

 

 

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/ContentFragment.html

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

 

HelpTech작성자
Level 3
October 15, 2024

Hi @martin_ecx_io 

 

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

 

I was unable to update the datefield still

rk_pandian
Level 4
October 17, 2024

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

kautuk_sahni
Community Manager
Community Manager
November 5, 2024

@helptech 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