Thursday, December 25, 2008

Users vs AllUsers vs SiteUsers

Scenario:
You want to get all the users who have access to the site. An SPWeb object exposes three different collections of users, as shown in this code below.

Code:

SPWeb web = SPContext.Current.Web;
SPUserCollection c1 = web.Users;
SPUserCollection c2 = web.AllUsers;
SPUserCollection c3 = web.SiteUsers;
Explaination:
The Users collection has the smallest membership of these three collections. This collection includes all the external principals that have been explicitly assigned permissions within the current site.

The AllUsers collection includes all members of the Users collection, plus external users that have accessed objects within the site using implicit permissions through group or role membership. For example, imagine a user named Sandeep with the login of LITWAREINC\SandeepN that has never been given explicit permissions to access a site and view a particular list. However, he might still be able to view the list because of his membership within an Active Directory group that has been configured with list view permissions. When Sandeep first accesses the site or one of its objects (say, a list using implicit permissions), he is added as a member of the AllUsers collection, but he is not added as a member of the Users collection.

The SiteUsers collection is an aggregation that combines membership for each AllUsers collection within the current site collection. The membership of this collection includes all external principals that have been assigned permissions to any object within the site collection as well as all external users that have been granted access to any of the site collection's objects using implicit permissions.

6 comments:

Anonymous,  January 29, 2009 at 10:28 PM  

Thanks Dear.
That was helpful!

Anonymous,  December 22, 2010 at 4:35 AM  

Programmtically how can we filter all unique users from all site collections within a web application?We have a requirement wherein we need only unique users across all site collections in a web application.

Thanks
Mohan

Anonymous,  December 23, 2010 at 12:00 AM  

What will the sharepoint UserInfoList contain - AllUsers or SiteUsers or Users?

Thanks
Mohan

Sandeep December 23, 2010 at 11:36 AM  

Actually it contains cached information about the users logged in at least once to the site collection

Yayati July 24, 2017 at 9:31 AM  

Thanks for detailed explanation. But one doubt which I want to clear. If a user assigned permission via a AD group in a site and he has never visited that site, then will I get that user instance through web.SiteUsers object?

Sandeep July 24, 2017 at 10:48 AM  

No you will not get his details. If you really need this info, you need to read the AD group users using Active Directory APIs