SPWeb: Difference between AllUsers and Users Property
Quick Quiz: Recently, I wrote a very simple web part that returns a list of users who have access to the current SharePoint site. The code snippets below show two approaches I tried. Do you know the difference in the results?
Method 1:
SPWeb currentSite = SPContext.Current.Web;
SPUserCollection userlist = currentSite.AllUsers;
foreach (SPUser user in userlist)
{
...
}
Method 2:
SPWeb currentSite = SPContext.Current.Web;
SPUserCollection userlist = currentSite.Users;
foreach (SPUser user in userlist)
{
...
}
SPWeb.AllUsers returns “the collection of user objects that represents all users who are either members of the site or who have browsed to the site as authenticated members of a domain group in the site”
SPWeb.Users returns “the collection of user objects that are explicitly assigned permissions in the Web site”
Big difference!... the easiest way to know which is being used is to remove permissions for an existing user (who has visited the site). He/She will continue to be appear when AllUsers is used; gone if Users is used.