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.
Views
Replies
Total Likes
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:
I already tried this method but the value is stored as below:
I was unable to update the datefield still
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
@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!
Views
Replies
Total Likes