Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Lukas Barina
How to programatically assign existing user group as a site member
February 22, 2013 5:04 AM
Answer

Lukas Barina

Rank: New Member

Posts: 8

Join Date: June 7, 2012

Recent Posts

Hi,
I cannot find a way how to programatically assign new members to a site.

Version of liferay used : 6.1.1

1, I am creating new site from a template - already ok - everything works

newSite = GroupLocalServiceUtil.addGroup(themeDisplay.getUserId(), Group.class.getName(), 0, myFirstSite,
"test site", GroupConstants.TYPE_SITE_OPEN, "/" + myFirstSite, true, true, serviceContext);
SitesUtil.applyLayoutSetPrototypes(newSite, 0, myLayoutPrototype.getLayoutSetPrototypeId(),
serviceContext);

2, I have existing user groups with users (imported automatically from ldap) - no problem all user groups and members visible in liferay

3, I want to assign existing user group to my freshly created site - I am lost how to do it , not sure about what API to use

Role role = RoleLocalServiceUtil.getRole(themeDisplay.getCompanyId(), "Site Member");
long[] roles = new long[] { role.getRoleId() };

I tried this method but seems to not working:
UserGroupGroupRoleLocalServiceUtil.addUserGroupGroupRoles(myUserGroup.getUserGroupId(), newSite.getGroupId(), roles);

Thanks for your help here

-Lukas
Chintan Akhani
RE: How to programatically assign existing user group as a site member
February 22, 2013 11:30 AM
Answer

Chintan Akhani

Rank: Regular Member

Posts: 111

Join Date: March 13, 2008

Recent Posts

Hi Lukas,

As per my knowledge, you have used right API for assigning 'Site Member' role to user group. Only thing which might be a problem here is 'myUserGroup.getUserGroupId()', can you please check whether you are passing right user group id or not.
Lukas Barina
RE: How to programatically assign existing user group as a site member
February 24, 2013 11:05 PM
Answer

Lukas Barina

Rank: New Member

Posts: 8

Join Date: June 7, 2012

Recent Posts

Hi Chintan,
I am 100% sure I am passing the correct user group id, because before this code I am printing
infomartions about all existing user groups and all their members.
And just before this code I am printing againg the user group id.

Just wondered if this method is the right one for assinging of a user group to a site...

In the below thread I found that there is some other possible way for assigning members to communities (old name of sites)
but it's a class from struts package not accessible in my portlet so I guess for me not usable at all:
EditGroupAssignmentsAction

http://www.liferay.com/community/forums/-/message_boards/view_message/4974302#_19_message_4954382

Thanks for any comments or if you have already done some assignments of users into the sites please send
your example code

--Lukas
Lukas Barina
RE: How to programatically assign existing user group as a site member
February 25, 2013 2:15 AM
Answer

Lukas Barina

Rank: New Member

Posts: 8

Join Date: June 7, 2012

Recent Posts

Hi again,
I found the solution, I just used not proper class from API
So to assign existing user group to a site you have to use following API code:

long[] groups = new long[] { userGroup.getUserGroupId() };

UserGroupLocalServiceUtil.addGroupUserGroups(newSite.getGroupId(), groups);

--Lukas