Expand my Community achievements bar.

Submissions are now open for the 2026 Adobe Experience Maker Awards.
SOLVED

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

Avatar

Level 1

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

JavedKh_0-1753432535449.png

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

1 Accepted Solution

Avatar

Correct answer by
Level 4

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.

 

 

View solution in original post

2 Replies

Avatar

Correct answer by
Level 4

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.

 

 

Avatar

Community Advisor

Hi @JavedKh 

You can check the sample Model which returns clientlibs based on category

https://github.com/arunpatidar02/aemaacs-aemlab/blob/master/core/src/main/java/com/community/aemlab/... 

Arun Patidar

AEM LinksLinkedIn