Mark
Mark

Reputation: 18827

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

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

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

Upvotes: 12

Views: 36007

Answers (5)

Piratenlulatsch
Piratenlulatsch

Reputation: 75

For anyone looking this up in 2023:

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

Upvotes: 0

FilippoG
FilippoG

Reputation: 339

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;
}

Upvotes: 0

Prakash K
Prakash K

Reputation: 11698

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.

Upvotes: 14

user832497
user832497

Reputation:

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(); 

Upvotes: 22

simplysiby
simplysiby

Reputation: 584

You can use the following :

GroupLocalServiceUtil.getCompanyGroup(PortalUtil.getDefaultCompanyId()).getGroupId();

Upvotes: 8

Related Questions