Skip to main content
Jul 25, 2025 8:37 AM UTC
Solved

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

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?

Karishma_begumSh
Karishma_begumShAccepted solution
Level 4
Jul 25, 2025 9:49 AM UTC

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
Jul 25, 2025 9:49 AM UTC

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.

 

 

Translate
arunpatidar
Community Advisor
Community Advisor
Jul 25, 2025 10:51 AM UTC
Arun Patidar