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=\"[email protected]\" 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
    Commented Nov 24, 2016 at 17:10
  • @danyseban the user Id "31" is an example. Its stated in the top of the question. Commented Nov 25, 2016 at 11:46

5 Answers 5


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. Commented Apr 10, 2014 at 6:42
  • you used web.SiteUsers.GetByID(31) that's why it worked instead of web.Users..GetByID(31) Commented Apr 10, 2014 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. Commented Apr 10, 2014 at 6:48
  • it worked for you which is more important :) Commented Apr 10, 2014 at 6:51
  • 1
    oh.but glad it worked for you today Commented Apr 10, 2014 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.
  • I should add that to the question, I've tried using EnsureUser. But thanks! Commented Apr 9, 2014 at 14:04
  • have you added full loginname(with domain) while using ensure user?
    – Unnie
    Commented Apr 9, 2014 at 14:16
  • Yes, while trying that method I looked at Tobias Zimmergrens blog post about hidden SharePoint User Group: zimmergren.net/technical/… Commented Apr 9, 2014 at 14:18
  • SPUser user = SPContext.Current.Web.EnsureUser(@"ZIMMERTobiasZimmergren"); Commented Apr 9, 2014 at 14:19
  • @George chk this stackoverflow.com/questions/3713497/…
    – Unnie
    Commented Apr 9, 2014 at 17:31

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

SPUser approver = web.SiteUsers.GetByEmail("[email protected]");
  • Tried it and the same result. It's basically the same method as GetById. Commented Apr 9, 2014 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? Commented Apr 9, 2014 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. Commented Apr 9, 2014 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
    Commented Apr 10, 2014 at 14:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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