Introduction
This article is to provide some Important code snippets for Liferay developers. It can be used to find lots of context information like the current user, current site, languageId, userId, groupId, permissionChecker, etc. Below code snippets can be helpful to Liferay developers in their day to day life.
1) Get Theme display object
// Theme display object //
import com.liferay.portal.theme.ThemeDisplay;
.
.
.
ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
WebKeys.THEME_DISPLAY);
2) Get current site GroupId
// Current site GroupId //
themeDisplay.getScopeGroupId();
3) Get Global site GroupId
// GLobal site GroupId //
themeDisplay.getCompanyGroupId();
4) Get current User
// Current User //
themeDisplay.getUserId(); //get userID
themeDisplay.getUser().getFirstName(); //get userName
5) Get logged In UserId
// Logged In UserId //
themeDisplay.getRealUserId();
6) Get current time zone
// Current Time Zone //
themeDisplay.getTimeZone(); // universal timezone
7) Get permission checker object
// Permission checker object //
themeDisplay.getPermissionChecker();
8) Get portlet display object
// Portlet display object //
themeDisplay.getPortletDisplay();
9) Get current portlet name
// Current portlet name //
themeDisplay.getPortletDisplay().getPortletName();
10) Get current portlet Configuration Url
// Current portlet Configuration Url //
themeDisplay.getPortletDisplay().getURLConfiguration();
11) Get portal layout ID
// Portal layout ID //
themeDisplay.getPlid();
12) Get languageId
// LanguageId //
themeDisplay.getLanguageId();
13) Get locale
//Locale //
themeDisplay.getLocale();
14) Get Layout
// Layout //
themeDisplay.getLayout();
15) Get logged In user’s Contact object
// Logged in User's Contact object //
themeDisplay.getContact();
16) Get current color scheme
// Current color scheme //
themeDisplay.getColorScheme()
17) Get current Company object
// Current Company object //
themeDisplay.getCompany();
18) Get current URL
// Current URL //
themeDisplay.getURLCurrent();
if your public page’s URL is /test then above method will give output like : “/web/guest/test”
19) Get portal URL
// Portal URL //
themeDisplay.getPortalURL(); // Example :- http://localhost:8080
20) Fetch categories from global site’s vocabulary by Vocabulary name
// Categories from global site’s vocabulary by Vocabulary name //
long globalGroupId = themeDisplay.getCompanyGroupId();
AssetVocabulary vocabulary = AssetVocabularyLocalServiceUtil.getGroupVocabulary(globalGroupId,“GLOBAL_VOCABULARY_NAME”);
List categories = AssetCategoryLocalServiceUtil.getVocabularyCategories(AssetCategoryConstants.DEFAULT_PARENT_CATEGORY_ID, vocabulary.getVocabularyId(), QueryUtil.ALL_POS,QueryUtil.ALL_POS, null);
21) Fetch categories from current site’s vocabulary by Vocabulary name
// Categories from current site’s vocabulary by Vocabulary name //
long currentGroupId = themeDisplay.getScopeGroupId();
AssetVocabulary vocabulary = AssetVocabularyLocalServiceUtil.getGroupVocabulary(currentGroupId ,“VOCABULARY_NAME”);
List categories = AssetCategoryLocalServiceUtil.getVocabularyCategories(AssetCategoryConstants.DEFAULT_PARENT_CATEGORY_ID, vocabulary.getVocabularyId(), QueryUtil.ALL_POS,QueryUtil.ALL_POS, null);
22) Fetch Journal Article object by urlTitle
// Journal Article object by urlTitle //
long groupId = themeDisplay.getScopeGroupId();
JournalArticle article = JournalArticleLocalServiceUtil.fetchArticleByUrlTitle(groupId, "urlTitle");
23) Fetch service context object in portlet class
// Service context object in portlet class //
ServiceContext serviceContext = ServiceContextFactory.getInstance(ServiceContext.class.getName(), request);
24) Fetch template object in portlet class by templateKey
// Template object in portlet class by templateKey //
long groupId = themeDisplay.getScopeGroupId();
long classNameId = ClassNameLocalServiceUtil.getClassNameId(JournalArticle.class);
String templateKey = "38427";
DDMTemplate template = DDMTemplateManagerUtil.fetchTemplate(groupId, classNameId, templateKey);