Need to fetch metadata properties of assets through sling models | Community
Skip to main content
lovepreetk75597
Level 2
May 6, 2019
Solved

Need to fetch metadata properties of assets through sling models

  • May 6, 2019
  • 2 replies
  • 2205 views

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

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 arunpatidar

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;

}

}

2 replies

arunpatidar
Community Advisor
arunpatidarCommunity AdvisorAccepted solution
Community Advisor
May 6, 2019

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
smacdonald2008
Level 10
May 6, 2019

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