Lincolnmyth
Lincolnmyth

Reputation: 63

Create sites(groups) programmatically in liferay

The past week I've been trying to make this work but I just can't get it done myself(I'm new to both java and liferay). So I was hoping someone could help.

I'm trying to make sites in liferay with the press of a button so users can create their own. Here's some of the code I have already:

public void addGroup(ActionRequest request, ActionResponse response)  {
    
    ThemeDisplay theme = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);  
    
    long userId = theme.getUserId();
    
    String friendly = "/hilversum";
    
    String name = "hilversum";
    
    Map<Locale, String> nameMap = new HashMap<Locale, String>();
    nameMap.put(LocaleUtil.NETHERLANDS, name);
    
    try {
        GroupLocalServiceUtil.addGroup( userId, GroupConstants.DEFAULT_PARENT_GROUP_ID, "", 37632, GroupConstants.DEFAULT_LIVE_GROUP_ID, 
                nameMap, null, GroupConstants.TYPE_SITE_OPEN, false, GroupConstants.DEFAULT_MEMBERSHIP_RESTRICTION, friendly, true, true, new ServiceContext() );
    } catch (PortalException e) {
        
        e.printStackTrace();
    }
    
}

This method is used in my jsp when someone presses submit on a form with a portlet actionURL. When I press the button however I get an exception: com.liferay.portal.kernel.exception.GroupKeyException at com.liferay.portal.service.impl.GroupLocalServiceImpl.validateGroupKey(GroupLocalServiceImpl.java:4868) at com.liferay.portal.service.impl.GroupLocalServiceImpl.addGroup(GroupLocalServiceImpl.java:387) at com.liferay.portal.service.impl.GroupLocalServiceImpl.addGroup(GroupLocalServiceImpl.java:481)

Am I doing something wrong in my code? or are the variables I give to the addGroup method wrong?

Upvotes: 2

Views: 343

Answers (1)

Pedro Blandim
Pedro Blandim

Reputation: 380

The nameMap you pass to addGroup method should have at least your default locale. Try replacing

    nameMap.put(LocaleUtil.NETHERLANDS, name);

by

    nameMap.put(LocaleUtil.getDefault(), name);

And don't forget to translate the name accordingly to the default locale.

LocaleUtil package is

    import com.liferay.portal.kernel.util.LocaleUtil;

obs.: I'm using Liferay version 7.3.10 GA1

Upvotes: 3

Related Questions