Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.

Creating Content Fragments Programmatically with some custom metadata properties in the same metadata node.

Avatar

Community Advisor

All,

 

I am creating content fragments programmatically and adding some custom metadata properties in the same node . The below code runs fine .The content fragment is created  and custom metadata properties added successfully in local and DEV environment ,

 

But the content fragment is created successfully and failed to add custom metadata properties in QA and other higher environment. 

 

//creating the content fragment

Resource templateOrModelRsc = resolver.getResource(contentfragmentTemplate);
FragmentTemplate fragmentTemplate = templateOrModelRsc.adaptTo(FragmentTemplate.class);
ContentFragment newFragment = fragmentTemplate.createFragment(parentRsc, fragmentName, fragmentTitle);
resolver.commit();

// adding some custom metadata attributes
Resource res = resolver.getResource(PATH_JCR_METADATA);
Node contentNode = res.adaptTo(Node.class);
if (Objects.nonNull(contentNode)) {

         setContent(newFragment, rowData);

         setMetadata(contentNode, metaData, resolver, path, newFragment);
resolver.commit();
}

 

 Any help is appreciated.

 

Thanks

Kannan

6 Replies

Avatar

Community Advisor

@KannanC : As it is already working fine lower environments, Did you check error log in Higher environments. Definitely there will be some errors you can see.

 

Otherwise , you can Put the logic of adding metadata in try block and you can catch the error and log the error.

 

Thanks,

Siva

 

 

Thanks,
Siva

Avatar

Community Advisor

Hi @SivakumarKanoori 

 

My code is already in try catch block only ..I didn't get any errors and exceptions ..I could not found any clue on that ..that's the issue ..

 

Thanks 

Kannan

Avatar

Community Advisor

@KannanC Can you please share the error log? Also, one suggestion is: you can use Sling API instead of JCR API to update properties.

Example: 

 Resource resource = resolver.getResource("/content/sourcedcode/jcr:content");
    if (Objects.nonNull(resource)) {
        ModifiableValueMap map = resource.adaptTo(ModifiableValueMap.class);
        map.put("newProperty""value");
        map.put("existingProperty""value");
        map.remove("existingProperty"); //remove
        resolver.commit();
    }

Avatar

Community Advisor

Hi @Siva_Sogalapalli 

 

I checked error logs already ..I didn't get any errors and exceptions ..

 

Any specific reason for using Sling API instead of JCR API ?

 

Thanks

Kannan 

 

 

Avatar

Community Advisor

It might be because in the QA environment, your currentUser who's using the resolver does not have permission to create such nodes?
its best practice to use system user in this case when trying to create some sort of utils for the AEM environment. See how to create system users for AEM here Create System | Service Users in AEM 6.5 with Code Configurations - Sourced Code

Avatar

Community Advisor

Hi All , 

 

The metadata properties are stored properly .But after storing , the custom workflow logic removed the metadata property immediately. This is the issue.

 

We have fixed the issue .

 

Thanks 

Kannan