How to add date field in the content fragment which is created programatically
Solved! Go to Solution.
Views
Replies
Total Likes
@nivethaS You can follow the below for creation of CFs programmatically.
Resource fragmentResource = resourceResolver.getResource(fragmentPath); if (null != fragmentResource) { ContentFragment contentFragment = fragmentResource.adaptTo(ContentFragment.class); if (null != contentFragment) { Iterator<ContentElement> elements = contentFragment.getElements(); while (elements.hasNext()) { ContentElement contentElement = elements.next(); if (contentElement.getValue().getDataType().isMultiValue()) { String[] values = contentElement.getValue().getValue(String[].class); String value = null; if (values != null) { value = StringUtils.join(values, CommonConstants.CHAR_COMMA); cfElements.put(contentElement.getName(), value); } } else { cfElements.put(contentElement.getName(), contentElement.getContent()); } } } }
Some other helpful links :
Hi,
use below code to create date field in custom CF
@component
public class CustomContentFragment {
@reference
private ResourceResolverService resourceResolverService;
public void createContentFragment(String fragmentPath, String dateFieldValue) {
ResourceResolver resourceResolver = resourceResolverService.getResolver();
try {
if (resourceResolver.hasChanges()) {// Check if the path exists
resourceResolver.commit();
}
Now Create the required content fragment
Resource fragmentResource = resourceResolver.create(
resourceResolver.getResource("/content/<your-path>"),
"<your-fragment-name>",
null // Add properties
);
//Now set the date field
fragmentResource.adaptTo(ValueMap.class).put("<yourDateField>", dateFieldValue);
resourceResolver.commit();
} catch (Exception e) {
// Handle exceptions
} finally {
resourceResolver.close();
}
}
}
. Date format value: YYYY-MM-DDTHH:mm:ssZ
Hope this helps!
Hi @HeenaMadan
I need to create a new CF and add the date field with value to it. In this case i need to create model as well. The ResourceResolverService import is not happening
Views
Replies
Total Likes
@nivethaS You can follow the below for creation of CFs programmatically.
Resource fragmentResource = resourceResolver.getResource(fragmentPath); if (null != fragmentResource) { ContentFragment contentFragment = fragmentResource.adaptTo(ContentFragment.class); if (null != contentFragment) { Iterator<ContentElement> elements = contentFragment.getElements(); while (elements.hasNext()) { ContentElement contentElement = elements.next(); if (contentElement.getValue().getDataType().isMultiValue()) { String[] values = contentElement.getValue().getValue(String[].class); String value = null; if (values != null) { value = StringUtils.join(values, CommonConstants.CHAR_COMMA); cfElements.put(contentElement.getName(), value); } } else { cfElements.put(contentElement.getName(), contentElement.getContent()); } } } }
Some other helpful links :
@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!
Views
Replies
Total Likes
Views
Likes
Replies