Reputation: 60203
I want to get the usernames (Strings) of all users in Alfresco.
My code is running as an AMP within Alfresco itself.
How to do that?
I tried using PersonService but it outputs no results, even though I have several users in Alfresco (created via the Share admin interface):
RunAsWork runAsWork = new RunAsWork() {
@Override
public Object doWork() throws Exception {
return personService.getPeople("*", null, null, new PagingRequest(1000));
}
};
PagingResults<PersonInfo> results =
(PagingResults<PersonInfo>) AuthenticationUtil.runAsSystem(runAsWork);
System.out.println("Number of users: " + results.getTotalResultCount());
while (results.hasMoreItems()) {
for (PersonInfo info : results.getPage()) {
System.out.println("User: " + info.getUserName());
}
}
Upvotes: 1
Views: 925
Reputation: 51
PersonService personService = documentService.serviceRegistry.getPersonService();
PagingResults<PersonInfo> users = personService.getPeople("*", new ArrayList<QName>(), new ArrayList<Pair<QName,Boolean>>(), new PagingRequest(personService.countPeople()));
logger.info("The number of users in the system" + personService.countPeople());
do {
List<PersonInfo> personInfos = users.getPage();
for(PersonInfo personInfo : personInfos) {
logger.info("User Name: " + personInfo.getUserName());
}
} while(users.hasMoreItems());
This code will log all the users in alfresco
Upvotes: 1
Reputation: 121
It might be an issue with the authentication from your amp, you may try this: (PagingResults) AuthenticationUtil.runAs(runAsWork, AuthenticationUtil.getSystemUserName());
It is the same thing as you did but I had trouble with runAsSystem before on an old repo. Else there are more way of messing up with the person service here PersonService.PersonInfo
Upvotes: 0
Reputation: 1348
The people api returns list of users
http://server:port/alfresco/service/api/people
or try this by injecting
serviceRegistry
try {
Set<String> authorities = serviceRegistry.getAuthorityService().getAllAuthorities(AuthorityType.USER);
for (String authority : authorities) {
NodeRef person = serviceRegistry.getPersonService().getPerson(authority);
Map<QName, Serializable> properties = serviceRegistry.getNodeService().getProperties(person);
String fullName=properties.get(ContentModel.PROP_FIRSTNAME) +" "+properties.get(ContentModel.PROP_LASTNAME);
System.out.println("User Full Name "+fullName);
}
} catch (Exception e) {
e.printStackTrace();
}
Hope this will help you.
Upvotes: 0