Legacy Knowledge Base
Published Sep. 10, 2025

Calling OSGI Service in fragments

Written By

Marcos da Silva Xavier

How To articles are not official guidelines or officially supported documentation. They are community-contributed content and may not always reflect the latest updates to Liferay DXP. We welcome your feedback to improve How To articles!

While we make every effort to ensure this Knowledge Base is accurate, it may not always reflect the most recent updates or official guidelines.We appreciate your understanding and encourage you to reach out with any feedback or concerns.

Legacy Article

You are viewing an article from our legacy "FastTrack" publication program, made available for informational purposes. Articles in this program were published without a requirement for independent editing or verification and are provided"as is" without guarantee.

Before using any information from this article, independently verify its suitability for your situation and project.

Issue

  • Is there an option/API available to call OSGI Services directly in fragments like it's possible using serviceUtil and staticUtil?

Environment

  • DXP 7.4

Resolution

  • Yes, you can obtain this by using serviceLocator inside the HTML code of your fragment, same as it is used inside free-markers.

    The only thing is changing inside fragments is that < changes for [ and > for ], like this:
     
    [#assign exampleServiceUtil = serviceLocator.findService("com.liferay.example.service.ExampleService") /]

    Keep in mind that serviceLocator is restricted by default for security reasons. In order to enable it, you need to follow this steps: Changes in Enabling serviceLocator Calls in Liferay DXP 7.0

    It is important to understand that activating serviceLocator from a security point of view, is considered a bad idea because it opens the access to all of your services from freemarker. That means that you explicitly trust anybody who's allowed to write freemarker templates to run server-side code (and have access to the LocalService APIs)

    Because of that, we recommend using TemplateContextContributors that helps you make available only what you need. See Injecting Additional Context Variables and Functionality into Your Theme Templates

    The code is very simple, and you can inject whatever you need, see this example: Template Context Contributor

    Other article that might be of your interest: Non service builder generated services are not retrieved by ServiceLocator in freemarker template

 

 

Did this article resolve your issue ?

Legacy Knowledge Base