Adding members to a site uses the createSiteMembership method of the SitesApi.
For more information about this ReST API endpoint, see Add Members to a Site.
For a description of the common parameters, such as fields, see Common Parameters.
import org.alfresco.core.handler.SitesApi;
import org.alfresco.core.model.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.util.List;
import static org.alfresco.core.model.SiteMembershipBodyCreate.RoleEnum.SITECOLLABORATOR;
@Component
public class AddSiteMembersCmd {
static final Logger LOGGER = LoggerFactory.getLogger(AddSiteMembersCmd.class);
private List<String> fields = null;
@Autowired
SitesApi sitesApi;
public void execute(String siteId, String personId) throws IOException {
SiteMembershipBodyCreate siteMembershipBodyCreate = new SiteMembershipBodyCreate();
siteMembershipBodyCreate.setId(personId);
siteMembershipBodyCreate.setRole(SITECOLLABORATOR);
SiteMemberEntry siteMemberEntry = sitesApi.createSiteMembership(siteId, siteMembershipBodyCreate, fields).getBody();
LOGGER.info("Created site membership {}", siteMemberEntry);
}
}
Executing this code will add a user with passed in ID with role Site Collaboraton to site with passed in ID:
% java -jar target/rest-api-0.0.1-SNAPSHOT.jar add-site-member test test
2021-05-05 13:21:31.290 INFO 17933 --- [ main] o.a.tutorial.restapi.RestApiApplication : Started RestApiApplication in 2.923 seconds (JVM running for 3.377)
2021-05-05 13:21:31.291 INFO 17933 --- [ main] o.a.tutorial.restapi.RestApiApplication : args[0]: add-site-member
2021-05-05 13:21:31.292 INFO 17933 --- [ main] o.a.tutorial.restapi.RestApiApplication : args[1]: test
2021-05-05 13:21:31.293 INFO 17933 --- [ main] o.a.tutorial.restapi.RestApiApplication : args[2]: test
2021-05-05 13:21:31.697 INFO 17933 --- [ main] o.a.tutorial.restapi.AddSiteMembersCmd : Created site membership class SiteMemberEntry {
entry: class SiteMember {
id: test
person: class Person {
id: test
firstName: Test
lastName: User
displayName: Test User
description: null
avatarId: null
email: test@example.com
skypeId: null
googleId: null
instantMessageId: null
jobTitle: null
location: null
company: class Company {
organization: null
address1: null
address2: null
address3: null
postcode: null
telephone: null
fax: null
email: null
}
mobile: null
telephone: null
statusUpdatedAt: null
userStatus: null
enabled: true
emailNotificationsEnabled: true
aspectNames: null
properties: null
capabilities: class Capabilities {
isAdmin: false
isGuest: false
isMutable: true
}
}
role: SiteCollaborator
isMemberOfGroup: false
}
}