Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn More

View all

Sign in to view all badges

How can I programatically create a new version of an asset with new content?

Avatar

Avatar
Level 1
peter-g
Level 1

Likes

0 likes

Total Posts

13 posts

Correct Reply

0 solutions
View profile

Avatar
Level 1
peter-g
Level 1

Likes

0 likes

Total Posts

13 posts

Correct Reply

0 solutions
View profile
peter-g
Level 1

05-10-2020

I have 2 assets in the dam, lets call them A and B.

How can I programatically make a new version of A, and use the content of B as that new version?

 

AssetManager. createRevision makes a new version of A just fine, and I tried AssetManager.createAsset on the path of A using the stream I grabbed from asset B, but it doesnt go through.

 

A new version gets created but I'm having trouble adding B's content. It doesn't matter what happens to B as it will be deleted afterwards.

 

Thanks

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar
Give Back 3
MVP
Manjunath_K
MVP

Likes

139 likes

Total Posts

188 posts

Correct Reply

61 solutions
Top badges earned
Give Back 3
Give Back
Boost 50
Boost 5
Boost 3
View profile

Avatar
Give Back 3
MVP
Manjunath_K
MVP

Likes

139 likes

Total Posts

188 posts

Correct Reply

61 solutions
Top badges earned
Give Back 3
Give Back
Boost 50
Boost 5
Boost 3
View profile
Manjunath_K
MVP

05-10-2020

Hi @peter-g 

 

Yes, as you mentioned we need to use assetManager.createRevision() to create initial version of asset before replacing content of Asset A by Asset B. please check below code which will create version & replace content as expected.

 

try (ResourceResolver resourceResolver = request.getResourceResolver()){
AssetManager assetManager = resourceResolver.adaptTo(AssetManager.class);
Resource resourceAssetA = resourceResolver.getResource("/content/dam/test/jane_doe.jpg");
Resource resourceAssetB = resourceResolver.getResource("/content/dam/test/larry_spiller.jpg");

if(resourceAssetA!= null && resourceAssetB!= null) {
Asset assetA = resourceAssetA.adaptTo(Asset.class);
Asset assetB = resourceAssetB.adaptTo(Asset.class);
assetManager.createRevision(assetA, "message to create initial version of asset", assetA.getName());
Rendition renditionAssetB = assetB.getOriginal();
assetManager.createAsset(assetA.getPath(), renditionAssetB.getStream(), renditionAssetB.getMimeType(), false);
}

if(resourceResolver.hasChanges()){
resourceResolver.commit();
}
} catch (Exception e) {
e.printStackTrace();
}

 

 

Before creating version & replacing asset content :

 

before-version.PNG

 

 

 

After creating version & replacing asset content :

 

after-version.png

 

 

After deleting original asset(Asset B) from which content is copied from , it will not affect to asset(Asset A) which has copied content.

 

delete-version.PNG

 

 

-Manjunath

 

 

Answers (1)

Answers (1)

Avatar

Avatar
Give Back 5
Employee
vanegi
Employee

Likes

389 likes

Total Posts

377 posts

Correct Reply

147 solutions
Top badges earned
Give Back 5
Give Back 3
Give Back 10
Give Back
Boost 50
View profile

Avatar
Give Back 5
Employee
vanegi
Employee

Likes

389 likes

Total Posts

377 posts

Correct Reply

147 solutions
Top badges earned
Give Back 5
Give Back 3
Give Back 10
Give Back
Boost 50
View profile
vanegi
Employee

06-10-2020

Each time a page/asset is modified cq creates a version of the same. This modified resource modification time is set in jcr:lastModified property of the page/asset. Manipulation of this property can be done to save future date and activate page on that date though its not preferred way. You can store the future date as a property in the page/asset. Then you could write a workflow or a scheduled job which activates pages/assets with a future date.