How to move bulk assets from one folder to another folder in AEM by using Servlet ? | Community
Skip to main content
Level 2
July 15, 2023
Solved

How to move bulk assets from one folder to another folder in AEM by using Servlet ?

  • July 15, 2023
  • 1 reply
  • 1471 views

Hi Team,

 

I have a requirement to move all the assets from one folder to another folder by using Servlet.

Can you please help me with some reference?

 

Thanks in advance.

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 bilal_ahmad

Hi @kaibalyajena_123


To move your assets on a button click, follow these steps:

1. Create a list of asset paths where the assets are currently located and need to be moved.

2. When the button is clicked, iterate through the list of asset paths one by one.

3. For each asset path in the list:
a. Check if the asset exists at the given path.
b. If the asset exists:
- Identify the destination path where you want to move the asset.
- Extract the asset's name from its parent resource.
- Create the new path in the destination folder by combining the destination path and the asset's name.
- Move the asset from the current path to the new path.
- Optionally, update any references or properties related to the moved asset.
- Log a success message or perform any additional actions(with asset path or something that help you identify).
c. If the asset does not exist, handle the case appropriately (e.g., log an error message).

4. Repeat this process for each asset path in the list.


By following these steps, you can move your assets to the desired location by clicking the button, ensuring that each asset is moved individually.

Sample code ref:

 

//fetch all the asset paths List < String > assetPaths = getAssetPathsToMove(); for (String assetPath: assetPaths) { moveAsset(resourceResolver, assetPath); } ---------- resourceResolver.move(assetPath, newAssetPath); ---------- String query = "SELECT * FROM [cq:PageContent] AS pageContent WHERE ISDESCENDANTNODE(pageContent, '/content/myAppA') AND CONTAINS(pageContent.*, 'fileReference=\"" + assetPath + "\"')"; Iterator < Resource > result = resourceResolver.findResources(query, Query.JCR_SQL2); while (result.hasNext()) { Resource pageResource = result.next(); ModifiableValueMap pageProperties = pageResource.getChild("jcr:content").adaptTo(ModifiableValueMap.class); // Update the asset reference with the new path pageProperties.put("fileReference", assetPath.replace("/content/dam", "/content/dam/destination")); resourceResolver.commit(); }

 

 

Thanks

-Bilal

1 reply

bilal_ahmad
bilal_ahmadAccepted solution
Level 5
July 15, 2023

Hi @kaibalyajena_123


To move your assets on a button click, follow these steps:

1. Create a list of asset paths where the assets are currently located and need to be moved.

2. When the button is clicked, iterate through the list of asset paths one by one.

3. For each asset path in the list:
a. Check if the asset exists at the given path.
b. If the asset exists:
- Identify the destination path where you want to move the asset.
- Extract the asset's name from its parent resource.
- Create the new path in the destination folder by combining the destination path and the asset's name.
- Move the asset from the current path to the new path.
- Optionally, update any references or properties related to the moved asset.
- Log a success message or perform any additional actions(with asset path or something that help you identify).
c. If the asset does not exist, handle the case appropriately (e.g., log an error message).

4. Repeat this process for each asset path in the list.


By following these steps, you can move your assets to the desired location by clicking the button, ensuring that each asset is moved individually.

Sample code ref:

 

//fetch all the asset paths List < String > assetPaths = getAssetPathsToMove(); for (String assetPath: assetPaths) { moveAsset(resourceResolver, assetPath); } ---------- resourceResolver.move(assetPath, newAssetPath); ---------- String query = "SELECT * FROM [cq:PageContent] AS pageContent WHERE ISDESCENDANTNODE(pageContent, '/content/myAppA') AND CONTAINS(pageContent.*, 'fileReference=\"" + assetPath + "\"')"; Iterator < Resource > result = resourceResolver.findResources(query, Query.JCR_SQL2); while (result.hasNext()) { Resource pageResource = result.next(); ModifiableValueMap pageProperties = pageResource.getChild("jcr:content").adaptTo(ModifiableValueMap.class); // Update the asset reference with the new path pageProperties.put("fileReference", assetPath.replace("/content/dam", "/content/dam/destination")); resourceResolver.commit(); }

 

 

Thanks

-Bilal