Ask Questions and Find Answers
Important:
Ask is now read-only. You can review any existing questions and answers, but not add anything new.
But - don't panic! While ask is no more, we've replaced it with discuss - the new Liferay Discussion Forum! Read more here here or just visit the site here:
discuss.liferay.com
RE: Liferay 7.2 - How to add groups programmatically?
Hi,
I am trying to add groups and layouts programmatically in my custom portlet. Right now I am successfully adding layouts like this:
But I can't seem to find a good example for the groups. I am confused with some parameters from the GroupLocalServiceUtil.addGroup() method. Is there are any good example of how to add groups programmatically?
Thank you!
I am trying to add groups and layouts programmatically in my custom portlet. Right now I am successfully adding layouts like this:
public void addLayout(ActionRequest request, ActionResponse response) throws IOException, PortletException, PortalException {
ThemeDisplay theme = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);
long userId = theme.getUserId();
long groupId = theme.getSiteGroupId();
String name = ParamUtil.getString(request, "name");
LayoutLocalServiceUtil.addLayout(
userId,
groupId,
false,
0,
name,
null,
null,
LayoutConstants.TYPE_PORTLET,
false,
null,
new ServiceContext()
);
}
But I can't seem to find a good example for the groups. I am confused with some parameters from the GroupLocalServiceUtil.addGroup() method. Is there are any good example of how to add groups programmatically?
Thank you!
What kind of groups?
The term group is a bit ambiguous in Liferay. Do you want to add Usergroups or do you want to add sites/organisations?
The term group is a bit ambiguous in Liferay. Do you want to add Usergroups or do you want to add sites/organisations?
Hi Christoph,
After some debugging I found what could be the correct way to create a group programmatically. My method is the following:
I think this is creating a site/organization if I am not mistaken.
After some debugging I found what could be the correct way to create a group programmatically. My method is the following:
public void addGroup(ActionRequest request, ActionResponse response) throws IOException, PortletException, PortalException {
ThemeDisplay theme = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY); long userId = theme.getUserId();
String name = ParamUtil.getString(request, "name");
Map<locale, string> nameMap = new HashMap<locale, string>();
nameMap.put(LocaleUtil.US, name);
nameMap.put(LocaleUtil.PORTUGAL, name);
GroupLocalServiceUtil.addGroup(
userId,
GroupConstants.DEFAULT_PARENT_GROUP_ID,
null,
0,
GroupConstants.DEFAULT_LIVE_GROUP_ID,
nameMap,
null,
GroupConstants.TYPE_SITE_OPEN,
false,
GroupConstants.DEFAULT_MEMBERSHIP_RESTRICTION,
null,
true,
true,
new ServiceContext()
);
}</locale,></locale,>
I think this is creating a site/organization if I am not mistaken.
Copyright © 2025 Liferay, Inc
• Privacy Policy
Powered by Liferay™