Creating Content Fragments Programmatically with some custom metadata properties in the same metadata node. | Community
Skip to main content
Community Advisor
April 7, 2023

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

  • April 7, 2023
  • 4 replies
  • 1459 views

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

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

4 replies

SivakumarKanoori
Community Advisor
Community Advisor
April 7, 2023

@knan : 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
KNanCommunity AdvisorAuthor
Community Advisor
April 7, 2023

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

Siva_Sogalapalli
Community Advisor
Community Advisor
April 7, 2023

@knan 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();
    }

KNanCommunity AdvisorAuthor
Community Advisor
April 7, 2023

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 

 

 

BrianKasingli
Community Advisor and Adobe Champion
Community Advisor and Adobe Champion
April 8, 2023

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

KNanCommunity AdvisorAuthor
Community Advisor
April 11, 2023

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