Modify metadata asset programatically | Community
Skip to main content
Level 2
November 3, 2020

Modify metadata asset programatically

  • November 3, 2020
  • 3 replies
  • 3075 views

Hello,

I am trying to modify metadata/dc:description when I create an asset, but changes does not commited on CRX.

My asset is a PDF and my code:

Asset assetFitxer = assetManager.createAsset(pathDAM, inStream, mimeType, true);
Resource metadataRes = assetFitxer.adaptTo(Resource.class).getChild("jcr:content/metadata");

ModifiableValueMap map = metadataRes.adaptTo(ModifiableValueMap.class);
map.put("dc:description", newCode);
ResourceResolver rs = metadataRes.getResourceResolver();
rs.commit();

--> PDF's file has own metadata and I need to modify dc:description 

If I did commit before to change metadata and after it, changes do not save yet.

 

I think workflow dam asset does not end before I try to change dc:description, so metadata field has original value. 

 

Any suggestions?

Thanks! 

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

3 replies

SureshDhulipudi
Community Advisor
Community Advisor
November 3, 2020

can you try like, after creating the asset :

 

AssetManager assetManager = resourceResolver.adaptTo(AssetManager.class);

Asset assetFitxer = assetManager.createAsset(pathDAM, inStream, mimeType, true);

 

resourceResolver.commit();
resourceResolver.refresh();

 

Then get the resource and update/set the dc:description property

SureshDhulipudi
Community Advisor
Community Advisor
November 4, 2020
Are you getting any error? or what is happening with the changes? Can you debug and see the output result? or print out the lines failing?
Anudeep_Garnepudi
Community Advisor
Community Advisor
November 3, 2020

Hi @abcr1 

Try below code which I used a while ago. Hope this works for you as well.

AssetManager assetManager = resourceResolver.adaptTo(AssetManager.class);
Asset newAsset = assetManager.createAsset(assetPath, is, mimeType, true);
Node asssetNode = newAsset.adaptTo(Node.class);
Node jcr_content = (asssetNode != null && asssetNode.hasNode("jcr:content")) ? asssetNode.getNode("jcr:content") : null;
Node metadata = (jcr_content != null && jcr_content.hasNode("metadata")) ? jcr_content.getNode("metadata") : null;
metadata.setProperty("dc:description", "value");
metadata.getSession().save();

AG

AG
abcr1Author
Level 2
November 4, 2020
Hi @anudeep_garnepudi, it did not work. Thanks!
prashantonkar
Level 4
November 9, 2020
Just to rule out the DAM Update Asset workflow as the root cause of conflict. Can you disable the workflow and try if metadata update is happening ?