September 19, 2020
Estimated Post Reading Time ~

How to create a session from resourceResolver objects?

Sometimes we need a session to work with some special JCR API which we can,t use with the help of resourceResolver then we can create an instance of a session in the same class file using the object of resourceResolver. Below is the code snippet.

First, we need to inject the ResourceResolverFactory interface to create a new resourceResolver in AEM. You could inject using the SCR annotation @Reference or Sling Model annotation @Inject.

@Reference

private ResourceResolverFactory resolverFactory;

Now create the ResourceResolver object using resourceResolverFactory. This line of code will create a rsourceResolver with administrator login.

ResourceResolver resourceResolver=resolverFactory.getServiceResourceResolver(null);

Now will create the instance of Session using the object of resourceResolver.

Session session = resourceResolver.adaptTo(Session.class);



By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.