How to get global (company) group id in Liferay?
Asked Answered
A

5

12

How to get the global (company) group id in Liferay without accessing ThemeDisplay?

P.S.: with ThemeDisplay it is simple: themeDisplay.getCompanyGroupId().

Ancon answered 21/8, 2012 at 12:49 Comment(2)
If you don't have access to themeDisplay, you might want to give a few more details on where you actually need access to this information.Microtome
oh - according to the (accepted) answer it was even more simple than I though - or even more global. Guess my thoughts were too complex - good that you found it.Microtome
Z
22

When you have only one Company in your portal:

Company company = CompanyLocalServiceUtil.getCompanyByMx(PropsUtil.get(PropsKeys.COMPANY_DEFAULT_WEB_ID));
long globalGroupId = company.getGroup().getGroupId(); 
Zootoxin answered 21/8, 2012 at 14:16 Comment(2)
CompanyLocalServiceUtil.getCompanyByMx(..) where do you found this method! Great, Thanks!Ancon
I think it would be more correct to use CompanyLocalServiceUtil.getCompanyByWebId()Nd
C
14

Extending yellow's answer, you can find the company if you know some value of the Portal Instance (Company):

  1. If you know the webId of the Portal Instance, can find company by:

    String webId = "liferay.com"; // PropsUtil.get(PropsKeys.COMPANY_DEFAULT_WEB_ID)
    Company company = CompanyLocalServiceUtil.getCompanyByWebId(webId);
    long globalGroupId = company.getGroup().getGroupId();
    
  2. If you know the mail-domain of the Portal Instance, can find company by:

    String mailDomain = "liferay.com";
    Company company = CompanyLocalServiceUtil.getCompanyByMx(mailDomain);
    long globalGroupId = company.getGroup().getGroupId();
    
  3. If you know the virtual host of the Portal Instance, can find company by:

    String virtualHost = "localhost";
    Company company = CompanyLocalServiceUtil.getCompanyByVirtualHost(virtualHost);
    long globalGroupId = company.getGroup().getGroupId();
    

There are also other useful methods available to explore in CompanyLocalServiceUtil, for those who are interested.

Thanks Yellow for the lead, it was really helpful.

Chadwick answered 22/8, 2012 at 6:36 Comment(0)
H
8

You can use the following :

GroupLocalServiceUtil.getCompanyGroup(PortalUtil.getDefaultCompanyId()).getGroupId();
Hatching answered 1/10, 2012 at 12:5 Comment(0)
H
0

If you need this info for Document Library, you can use

public static long getDefaultCompanyId(){
        long companyId = 0;
        try{ companyId = getDefaultCompany().getCompanyId(); }
        catch(Exception e){ System.out.println(e.getClass() + " " +e.getMessage()); }
       return companyId;
}

public static long getDefaultGroupId (){

    long companyId = getDefaultCompanyId();
    long globalGroupId = 0L;

    Group group = null;
    try {
        group = GroupLocalServiceUtil.getGroup(companyId, "Guest");
    } catch (PortalException | SystemException e) {
        e.printStackTrace();
        return globalGroupId;
    }
     globalGroupId = group.getGroupId();


    return globalGroupId;
}
Hutcheson answered 14/7, 2017 at 8:50 Comment(0)
L
0

For anyone looking this up in 2023:

Just use: com.liferay.portal.kernel.util.PortalUtil.getDefaultCompanyId()

Leavelle answered 14/7, 2023 at 15:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.