SPWeb.SiteUsers vs SPWeb.Users

Update: I recently ran into this issue again and discovered that there is in fact a way to ensure that the user exists before creating the user object — the EnsureUser method of the SPWeb class. According to MSDN this method “Checks whether the specified login name belongs to a valid user of the Web site, and if the login name does not already exist, adds it to the Web site.” Check it out here. I tried this and it seems to resolve my issue. If the user is not a member of the web site, this method will add it which is effectively what I needed it to do.

Please note that you’ll need to set the AllowUnsafeUpdates property to true in order for the EnsureUser method to do it’s thing.

When trying to use SPWeb.Users to find a particular user in a site, I was receiving a “User cannot be found” error within the site that my method was being called from. I took a look at the site permissions and discovered that the user account I was attempting to add did not actually exist as a member of that site. At first glance, I didn’t actually think that this would be a problem, but apparently there is a key disctinction between the various SPWeb methods.

If I added the user to the site manually, the SPWeb.Users call worked. Any users automatically added to the site upon creation, will not be found with a SPWeb.Users call. This is an important disctinction.

My next attempt was to use SPWeb.SiteUsers which was only successful if the user existed at the site collection level. However, if the user was not a member of any group at the site collection level, my method would still fail with a “User cannot be found” error.

Alternatively, I might be able to leverage the user profile database to get a list of all possible users to choose from but I haven’t done any experimenting with this yet. If anyone has any additional information or feedback on this, please drop me a note.

Windows SharePoint Services 3.0 SDK

SPWeb.AllUsers – Gets 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.SiteUsers – Gets the collection of all users that belong to the site collection.

SPWeb.Users – Gets the collection of user objects that are explicitly assigned permissions on the Web site.

,

4 Responses to “SPWeb.SiteUsers vs SPWeb.Users”

  1. Todd Kitta December 8, 2008 at 6:18 pm #

    Did you find a sure fire way to access an SPUser object ANYWHERE in WSS (not MOSS)? I am striking out left and right…

  2. shereen December 8, 2008 at 6:19 pm #

    @Todd, I actually did find a solution, read the update on this post. What you need to do is leverage the EnsureUser method to gaurantee that the user will be added to the site if they do not already exist.

  3. Sachin April 8, 2014 at 8:08 pm #

    Hey Shereen,

    I am struggling with the below stuff, can you please help me on it.

    I need to retrieve the list of users who have access to site collection. I tried all three option and nothing is working properly.

    SPWeb.AllUsers – It gives me list of all users who are no longer access to my site collection.

    SPWeb.SiteUsers- It also behave the same way like SPWeb.AllUsers works.

    SPWeb.Users – It give me list of users who are explicitly added , it doesn’t give me list of users who currently have access to site collection.

    Any help is highly appreciated.

    Regards,
    Sachin

  4. Shereen Qumsieh April 28, 2014 at 5:32 pm #

    Hi Sachin,

    Sorry for the delayed reply! Have you tried using EnsureUser?

Leave a Reply