Expand my Community achievements bar.

SOLVED

Create date field in content fragment programatically

Avatar

Level 3

How to add date field in the content fragment which is created programatically

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

@HelpTech 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 : 

https://experienceleague.adobe.com/en/docs/experience-manager-65/content/implementing/developing/ext...

 

https://medium.com/@rajathannasi168/unlocking-the-power-of-aem-programmatic-content-fragment-creatio...

 

View solution in original post

4 Replies

Avatar

Community Advisor

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!

Avatar

Level 3

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 

Avatar

Correct answer by
Community Advisor

@HelpTech 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 : 

https://experienceleague.adobe.com/en/docs/experience-manager-65/content/implementing/developing/ext...

 

https://medium.com/@rajathannasi168/unlocking-the-power-of-aem-programmatic-content-fragment-creatio...

 

Avatar

Administrator

@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