Frequently used code snippets for Liferay

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