Expand my Community achievements bar.

July 31st AEM Gems Webinar: Elevate your AEM development to master the integration of private GitHub repositories within AEM Cloud Manager.
SOLVED

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

Avatar

Level 2

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.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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

View solution in original post

1 Reply

Avatar

Correct answer by
Community Advisor

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