How To get the etc.clientlib path with their Id's in AEM Sling Model | Community
Skip to main content
July 25, 2025
Solved

How To get the etc.clientlib path with their Id's in AEM Sling Model

  • July 25, 2025
  • 2 replies
  • 410 views

I have a clientlib path which is getting generated in <script> tag by using data-sly-call to a specific clientlib category in HTL file

Is there any way that we can get this etc.clientlib path with the proper id in Sling model?

Best answer by Karishma_begumSh

Hi @javedkh 

Yes, there are ways to generate the correct clientlib URL in your sling model using AEM API's,

 

 

Instead of HTL constructs.

You can inject the HtmlLibraryManager OSGi service into your Sling Model and call its getLibraryUrl method.

 

import com.adobe.granite.ui.clientlibs.HtmlLibraryManager; import com.adobe.granite.ui.clientlibs.LibraryType; import org.apache.sling.api.resource.ResourceResolver; import org.apache.sling.models.annotations.DefaultInjectionStrategy; import org.apache.sling.models.annotations.Model; import org.apache.sling.models.annotations.injectorspecific.Self; import org.apache.sling.models.annotations.injectorspecific.OSGiService; import javax.annotation.PostConstruct; @Model(adaptables = ResourceResolver.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL) public class ClientLibPathModel { @Self private ResourceResolver resourceResolver; @OSGiService private HtmlLibraryManager htmlLibraryManager; private String clientlibPath; @PostConstruct protected void init() { if (htmlLibraryManager != null) { clientlibPath = htmlLibraryManager.getLibraryUrl(resourceResolver, "category-name", LibraryType.JS); } } public String getClientlibPath() { return clientlibPath; } }

Hope this helpful....:)

Regards,

Karishma.

 

 

2 replies

Karishma_begumSh
Karishma_begumShAccepted solution
Level 4
July 25, 2025

Hi @javedkh 

Yes, there are ways to generate the correct clientlib URL in your sling model using AEM API's,

 

 

Instead of HTL constructs.

You can inject the HtmlLibraryManager OSGi service into your Sling Model and call its getLibraryUrl method.

 

import com.adobe.granite.ui.clientlibs.HtmlLibraryManager; import com.adobe.granite.ui.clientlibs.LibraryType; import org.apache.sling.api.resource.ResourceResolver; import org.apache.sling.models.annotations.DefaultInjectionStrategy; import org.apache.sling.models.annotations.Model; import org.apache.sling.models.annotations.injectorspecific.Self; import org.apache.sling.models.annotations.injectorspecific.OSGiService; import javax.annotation.PostConstruct; @Model(adaptables = ResourceResolver.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL) public class ClientLibPathModel { @Self private ResourceResolver resourceResolver; @OSGiService private HtmlLibraryManager htmlLibraryManager; private String clientlibPath; @PostConstruct protected void init() { if (htmlLibraryManager != null) { clientlibPath = htmlLibraryManager.getLibraryUrl(resourceResolver, "category-name", LibraryType.JS); } } public String getClientlibPath() { return clientlibPath; } }

Hope this helpful....:)

Regards,

Karishma.

 

 

arunpatidar
Community Advisor
Community Advisor
July 25, 2025