I'm unable to get SPUsers/user.

As an example I have here a User (Approver):

<User ID=\"31\" Sid=\"\" Name=\"Approver\" LoginName=\"i:0#.w|domain\\approver\"
Email=\"approver@domain.se\" Notes=\"\" IsSiteAdmin=\"False\" 
IsDomainGroup=\"False\" Flags=\"0\" />

I've tried a variety of different ways to get the users:

SPUser user = web.SiteUsers.GetByID(31);
SPUser user = web.Users.GetByID(31);
SPUser user = web.SiteUsers["LoginName"];
SPMember member = web.AllUsers["Domain\\User_Alias"]; 
SPUser user = web.EnsureUser("LoginName");

I run this with elevated privileges.

Still get the error: User cannot be found.

enter image description here

Any suggestions how to go about getting the SPUser Object. Or what can be of causing me not getting the users from the different methods above.

  • Warning with this code=> web.SiteUsers.GetByID(31); maybe you know but per web or site collection, id(31) not identically or if yes this hazard because in SQL each web is distinct
    – dany seban
    Nov 24 '16 at 17:10
  • @danyseban the user Id "31" is an example. Its stated in the top of the question. Nov 25 '16 at 11:46

Try this

                SPUserCollection users=web.SiteUsers;
                SPUser user=users.GetByID(31);

SPweb.User return only users who are directly set as user on the web, if someone have access to web because he is in the particular group, this user cannot be found. SPweb.SiteUsers return all users

  • So this is rather odd, SPUserCollection users = web.SiteUsers; SPUser user = users.GetByID(31); and SPUser user = web.SiteUsers.GetByID(31); are the same. Both work today. Apr 10 '14 at 6:42
  • you used web.SiteUsers.GetByID(31) that's why it worked instead of web.Users..GetByID(31) Apr 10 '14 at 6:43
  • 1
    If you are referring to my question those are just samples from what i tried. I of course tried SiteUsers.GetByID. But thanks for the answer never the less. Apr 10 '14 at 6:48
  • it worked for you which is more important :) Apr 10 '14 at 6:51
  • 1
    oh.but glad it worked for you today Apr 10 '14 at 7:04

Try any of the below:

  web.EnsureUser(@"domain\loginname");//This will add user to site if not already added.
                              //Best to use this if you have the login name and 
                              //user is already added.

  web.SiteUsers.GetByID(31);  //This will get you the user by id.

If you have the user's email address, give this method a try:

SPUser approver = web.SiteUsers.GetByEmail("approver@domain.se");
  • Tried it and the same result. It's basically the same method as GetById. Apr 9 '14 at 14:52
  • Could you create a console app or script that iterates over the SiteUsers collection to see if the user shows up and make sure you're actually able to access the collection. Also, does the user show up when you browse to the Site User List @ spsiteurl/_catalogs/users/simple.aspx? Apr 9 '14 at 19:07

I suggest check it in PowerShell

$web = get-spweb $weburl;
$web.siteusers | ft UserLogin, Id;

Then try to get that specific user by id

$user = $web.siteusers.getbyid(31);

If this works in PowerShell, it should work in your managed code.

SPUser approver = web.SiteUsers.GetById(31);

NOTE: One thing to be aware of is that a users Id will change from one site collection to another.


I think you may be missing the following line:

web.AllowUnsafeUpdates = true; 

Since you are ensuring the user is part of the website and if not, it adds that user to the site, then you need to allow unsafeupdates. Your code should be:

web.AllowUnsafeUpdates = true; 
SPUser user = web.EnsureUser(@"Domain\LoginName");
//do more code stuff
web.AllowUnsafeUpdates = false;
  • When getting items from SharePoint is this really necessary? I run the code with elevated privileges. Isn't that enough since I'm not performing an update on the SPUser Object. Apr 9 '14 at 21:49
  • 1
    Whoops I meant EnsureUser (edited). Since you are essentially adding (updating) the user to the site in code if the user is not present, you need that to execute correctly in the web context. It will only work without it if the user is already added. sharepoint.infoyen.com/2012/03/24/spweb-allowunsafeupdates
    – Mike
    Apr 10 '14 at 14:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.