Need to fetch metadata properties of assets through sling models

Avatar

Avatar

lovepreetk75597

Avatar

lovepreetk75597

lovepreetk75597

06-05-2019

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

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar

Arun_Patidar

MVP

Total Posts

2.9K

Likes

1.0K

Correct Answer

831

Avatar

Arun_Patidar

MVP

Total Posts

2.9K

Likes

1.0K

Correct Answer

831
Arun_Patidar
MVP

06-05-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;

}

}

Answers (1)

Answers (1)

Avatar

Avatar

smacdonald2008

Total Posts

12.7K

Likes

1.4K

Correct Answer

2.3K

Avatar

smacdonald2008

Total Posts

12.7K

Likes

1.4K

Correct Answer

2.3K
smacdonald2008

06-05-2019

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