Apps can restrict their data to specific scopes (e.g., Global, Site, Page). Here, you’ll learn how to
For more detailed information about scoping, see Data Scopes.
Enabling Scoping
-
Scope your app’s entities. In your service layer, your entities must have a
companyId
attribute of typelong
to enable scoping by portal instance, and agroupId
attribute of typelong
to enable scoping by Site. Using Service Builder is the simplest way to do this. For instructions on this, see Service Builder Persistence and Business Logic with Service Builder. -
To enable scoping in your app, set the property
"com.liferay.portlet.scopeable=true"
in your portlet class’s@Component
annotation. For example, the Web Content Display Portlet’s portlet class sets this component property:@Component( immediate = true, property = { ... "com.liferay.portlet.scopeable=true", ..., }, service = Portlet.class ) public class JournalContentPortlet extends MVCPortlet { ... }
Accessing Your App’s Scope
Users can typically set an app’s scope to a page, a Site, or the entire portal. To handle your app’s data, you must access it in its current scope. Your app’s scope is available in these ways:
-
Via the
scopeGroupId
variable injected in JSPs that use the<liferay-theme:defineObjects />
tag. This variable contains your app’s current scope. For example, the Liferay Bookmarks app’sview.jsp
uses itsscopeGroupId
to retrieve the bookmarks and total number of bookmarks in the current scope:... total = BookmarksEntryServiceUtil.getGroupEntriesCount(scopeGroupId, groupEntriesUserId); bookmarksSearchContainer.setTotal(total); bookmarksSearchContainer.setResults(BookmarksEntryServiceUtil.getGroupEntries(scopeGroupId, groupEntriesUserId, bookmarksSearchContainer.getStart(), bookmarksSearchContainer.getEnd())); ...
-
By calling the
getScopeGroupId()
method on the request’sThemeDisplay
. This method returns your app’s current scope. For example, the Liferay Blogs app’sEditEntryMVCActionCommand
class does this in itssubscribe
andunsubscribe
methods:protected void subscribe(ActionRequest actionRequest) throws Exception { ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute( WebKeys.THEME_DISPLAY); _blogsEntryService.subscribe(themeDisplay.getScopeGroupId()); } protected void unsubscribe(ActionRequest actionRequest) throws Exception { ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute( WebKeys.THEME_DISPLAY); _blogsEntryService.unsubscribe(themeDisplay.getScopeGroupId()); }
If you know your app always needs the portal instance ID, use
themeDisplay.getCompanyId()
. -
By calling the
getScopeGroupId()
method on aServiceContext
object. See Understanding Service Context for an example and more details. If you know your app always needs the portal instance ID, use theServiceContext
object’sgetCompanyId()
method.
Accessing the Site Scope
To access the Site scope regardless of your app’s current scope, use the
ThemeDisplay
method getSiteGroupId()
. For more information on this use case, see
Accessing the Site Scope Across Apps.
For example, the Web Content app’s
edit_feed.jsp
uses the getSiteGroupId()
method to get the Site ID, which is required to
retrieve Structures:
ddmStructure = DDMStructureLocalServiceUtil.fetchStructure(themeDisplay.getSiteGroupId(),
PortalUtil.getClassNameId(JournalArticle.class), ddmStructureKey, true);