Expand my Community achievements bar.

SOLVED

Need to fetch metadata properties of assets through sling models

Avatar

Level 3

Hi,

I have one requirement where i need fetch asset path or asset's metadata properties using sling models. Does anyone have any idea how could we achieve this.

Regards,

Lovepreet

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

you can try something like below:

@Model(adaptables = { Resource.class, SlingHttpServletRequest.class })

public class ImageMetadataModel {

@Inject

@Optional

private String assetPath;

@SlingObject

private ResourceResolver resourceResolver;

public String getProperty () {

Resource resource = resourceResolver.getResource(assetPath);

String property = “”;

if (resource != null) {

Asset asset = resource.adaptTo(Asset.class);

property = asset.getMetadataValue("dc:title");

}

return property;

}

}



Arun Patidar

View solution in original post

2 Replies

Avatar

Correct answer by
Community Advisor

you can try something like below:

@Model(adaptables = { Resource.class, SlingHttpServletRequest.class })

public class ImageMetadataModel {

@Inject

@Optional

private String assetPath;

@SlingObject

private ResourceResolver resourceResolver;

public String getProperty () {

Resource resource = resourceResolver.getResource(assetPath);

String property = “”;

if (resource != null) {

Asset asset = resource.adaptTo(Asset.class);

property = asset.getMetadataValue("dc:title");

}

return property;

}

}



Arun Patidar

Avatar

Level 10

As Arun has pointed out - you must use the Asset API to perform this use case.