Get all users in Umbraco v7.1.4
asp.net, c#, umbraco, umbraco7
Solution
The userservice is available in the Services collection. If you use the MVC views or partials, you can just find it back in Services. Otherwise you can find it in `ApplicationContext.Current.Services`
Another way to find it back is :
using Umbraco.Core.Services;
using Umbraco.Core.Persistence;
var userService = new UserService(new RepositoryFactory());
The current user can be found in `UmbracoContext.Current.Security.CurrentUser`
Update
To do a GetAll you could do this in a View:
@{
var userService = ApplicationContext.Current.Services.UserService;
int totalRecords;
}
@foreach (var user in userService.GetAll(0, 100, out totalRecords))
{
<h1>@(user.Name)</h1>
@user.Username
}
Number of users found: @totalRecords.ToString()
But... remember the Services will hit the database, if you want to build well performing website with lots of users, consider another solution.
Problem
I am trying to retrieve the details of all users in my Umbraco site. The purpose of this is so that a content creator can specify usernames as "Owners" of the content that can be contacted by anonymous users of the website (a simple "mailto" link using the stored email address after matching username with username given by content creator). I have been able to get user details using: ``` var users = umbraco.BusinessLogic.User.getAll(); ``` but I am prompted in Visual Studio by the following warning: ``` 'Umbraco.BusinessLogic.User' is obsolete: '"Use the UserService instead"' ``` I created an instance of the UserService but the GetAll() function requires a number of parameters that the BusinessLogic function does not. How do I get all users using the UserService? Is there a better way for me to achieve what I am trying to achieve? For clarification, this is the following code I have used with the deprecated BusinessLogic: ``` var users = umbraco.BusinessLogic.User.getAll(); var owners = Umbraco.Field("owner").ToString().Split(','); foreach (var user in users) { foreach (var owner in owners) { if (String.Equals(user.LoginName, owner)) { <div class="owner"> <a href="mailto:@user.Email">@owner</a> </div> } } } ```