Programmatically change asset name | Community
Skip to main content
This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by smacdonald2008

Try calling the MoveAsset method that belongs to this API:

https://docs.adobe.com/docs/en/cq/5-6-1/javadoc/com/adobe/granite/asset/api/AssetManager.html#moveAsset(java.lang.String, java.lang.String)

Parameters:
assetPath - Absolute path of the asset to be moved
destAbsPath - Absolute path at with asset is to be moved

for destAbsPath  - specify the new name of the Asset.

To learn how to use the granite AssetManager API - see this article: 

https://helpx.adobe.com/experience-manager/using/graniteAPI.html (tested on AEM 6)

8 replies

smacdonald2008
smacdonald2008Accepted solution
January 19, 2016

Try calling the MoveAsset method that belongs to this API:

https://docs.adobe.com/docs/en/cq/5-6-1/javadoc/com/adobe/granite/asset/api/AssetManager.html#moveAsset(java.lang.String, java.lang.String)

Parameters:
assetPath - Absolute path of the asset to be moved
destAbsPath - Absolute path at with asset is to be moved

for destAbsPath  - specify the new name of the Asset.

To learn how to use the granite AssetManager API - see this article: 

https://helpx.adobe.com/experience-manager/using/graniteAPI.html (tested on AEM 6)

TheBigRed
TheBigRedAuthor
January 19, 2016

smacdonald2008 wrote...

Try calling the MoveAsset method that belongs to this API:

https://docs.adobe.com/docs/en/cq/5-6-1/javadoc/com/adobe/granite/asset/api/AssetManager.html#moveAsset(java.lang.String, java.lang.String)

Parameters:
assetPath - Absolute path of the asset to be moved
destAbsPath - Absolute path at with asset is to be moved

for destAbsPath  - specify the new name of the Asset.

To learn how to use the granite AssetManager API - see this article: 

https://helpx.adobe.com/experience-manager/using/graniteAPI.html (tested on AEM 6)

 


Thank you Scott

kautuk_sahni
Community Manager
Community Manager
January 20, 2016

Reference link to show one more use of AssetManager :- https://helpx.adobe.com/experience-manager/using/uploading-files-aem1.html

Thanks and Regards

Kautuk Sahni

Kautuk Sahni
June 1, 2018

smacdonald2008 Do AssetManager.moveAsset() method update the asset references as well in all the content or do we need to specifically handle it by writing custom code? When using "Move" option from author instance in Assets, it does update the content references of that particular asset which is moved, so wanted to confirm does API handle it automatically as well?

smacdonald2008
June 1, 2018

If you mean does it update in all pages referencing it - the answer is no. That is --  if you have a page referencing an asset in the DAM and you change the name - the reference in the page is not updated,

June 1, 2018

I am actually trying to figure what AEM uses internally when "Move" option is used from Assets UI. When the author Move an asset from Assets UI to a new destination, then all the pages where that asset was referenced, are automatically updated by AEM with the new destination path of the Asset. I need to do this Move from through a script, so thought of confirming if any API is there, which does update the references automatically or will I have to run queries to find all references and then update those references manually.

Adobe Employee
June 4, 2018

You can use CopyMoveCommandBuilder.java from package "com.day.cq.wcm.command.api" on 6.4.

e.g. :

@Reference
CommandBuilderFactory commandBuilderFactory;

CopyMoveCommandBuilder copyMoveCommandBuilder= commandBuilderFactory.createCommandBuilder(CopyMoveCommandBuilder.class).withCopy(false);

CopyMoveCommandPathArgument copyMoveCmdPathArg =

  (CopyMoveCommandPathArgument) copyMoveCommandBuilder.createPathArgumentBuilder()

  .withSrcPath(srcPath)

  .withDstPath(dstPath)

  .withAdjustRefPaths(adjustRefs)

  .withPublishPaths(publishRefs)

  .build();
   copyMoveCommandBuilder.withResourceResolver(resolver)

  .withAssetReferenceResolver(assetRefResolver)

  .withRetrieveAllRefs(true).withShallow(true)

  .withCheckIntegrity(true).withBeforeName(before)

  .withPathArgument(copyMoveCmdPathArg);
}

Command cmd = copyMoveCommandBuilder.build();
CopyMoveCommandResult result = (CopyMoveCommandResult)cmd.execute();

You need collect all the references and provide to this.

August 17, 2021

asset reference resolver is null and 

throws exception in commandbuilder 

May 6, 2023

Is there a solution to this? 

@smacdonald2008 

All the links are expired. Is there an endpoint in the HTTP API to change asset name? Or is it possible to access the granite AssetManager API via things like postman?