Tuesday, February 24, 2009

What is the diffrence between AllUsers, SiteUsers, Users ?

In the SPWeb object you have those three properties that sounds a like, but there is a slight diffrence between them -

AllUsers - Returns "almost" all users in the site collection ( including "guests" ).

SiteUsers - Returns all users that are really exist in the site collection, including groups such as "authenticated users" and system accounts and etc.

Users - Returns only the users that appear in that specific web site.

The explanation in the SDK wasn't clear enough ( as for now , before the service packs ).

If you want to check it yourself just run this short code and see the difference.

 

//Open a SPWeb object ( a site collection )

using (SPWeb web = colSites[i].OpenWeb())
{
SPUserCollection colUsers = web.Users;
foreach (SPUser user in colUsers)
{
Console.WriteLine(user.Name);
}
Console.WriteLine("=======================");
SPUserCollection colUsers1 = web.SiteUsers;

foreach (SPUser user in colUsers1)
{
Console.WriteLine(user.Name);
}
Console.WriteLine("=======================");
SPUserCollection colUsers2 = web.AllUsers;

foreach (SPUser user in colUsers2)
{
Console.WriteLine(user.Name);
}


}

No comments:

Post a Comment