SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Am I missing something here? There should be a method to check if a user exist, which doesn't throw an error if it doesn't. EnsureUser() is not applicable, because that method creates a new user if it doesn't exist.

At the time, this is what I do, but I think it should be easier in some way.

var userName = @"DOMAIN\UserName"
var userString = string.Empty;
  SPUser user = web.AllUsers[userName];
  userString = user.ID + ";#" + user.LoginName;
catch (Exception e)
  //The user does not exist, but it's okay
  Console.WriteLine("The user does not exist, but it's okay: " +e.InnerException);
share|improve this question
I would probably just use the AllUsers collection too. – Kit Menke Mar 16 '11 at 13:53

Personally, I'd use this:

  var user = web.AllUsers.Cast<SPUser>().FirstOrDefault(u => u.LoginName == userName);
  if (user != null) 
share|improve this answer
This is big performance gap – IAfanasov Apr 25 '12 at 6:23

You should use:

using (SPSite site = new SPSite(_upa))

    SPServiceContext context = SPServiceContext.GetContext(site);
    UserProfileManager _upaManager = new UserProfileManager(context);

    // Chek if user exists
    if( _upaManager.UserExists("mydomain\\myuser")}
        // The user exists
        // The User doesn't exists
share|improve this answer
At UserProfileManager declaration it throws error if user profile doesn't exists: "An exception of type 'Microsoft.Office.Server.UserProfiles.UserProfileApplicationNotAvailableExceptio‌​n' occurred in Microsoft.Office.Server.UserProfiles.dll but was not handled in user code Additional information: UserProfileApplicationNotAvailableException_Logging :: UserProfileApplicationProxy.GetRawPartitionID has null proxy" – love thakker Apr 13 at 11:49

If you have an email address you could use GetByEmail then check for null - cleaner than throwing Exceptions I reckon.

share|improve this answer
Yep, James is correct that you can get it by email or id, and you have to do all that exception handling if you want to get user by login. Maybe it would been better if AllUsers["username"] could return null if it doesnt find such user, but it can be job for a extension method. – Andriy Shvay Mar 16 '11 at 8:30
I don't want to query for e-mail, since most people have several, and it's not certain which e-mail is associated with the user profile. – tarjeieo Mar 16 '11 at 8:52
In SP2007, GetByEmail will throw SPException if the email address is not found. – Bill Yang Aug 3 '11 at 20:57
@Bill: Just had a look, it seems to do that in 2010 as well... that's a pretty bad implementation to be honest. – James Love Aug 3 '11 at 21:07

I offer another practice - query UsesInfoList:

                            using (var site = p)
                                using (var root = site.RootWeb)
                                    var query = new SPQuery();
                                    query.ExpandUserField = false;
                                    query.Query =string.Format(
                                        @"<Where><Eq><FieldRef Name=""Name"" /><Value Type=""Text"">{0}</Value></Eq></Where>", 
                                    var items = root.SiteUserInfoList.GetItems(query);
                                    if (items == null || items.Count == 0)
                                    var userID = items.OfType<SPListItem>().First().ID;
                                    var user = root.SiteUsers.GetByID(userID);
                                    if (!user.LoginName.Equals(newLogin))
                                        throw new InvalidOperationException("Unable to get user id by CAML " + query.Query);
                                    user.Name = fullName;
                                    user.Email = newEmail;
                        catch (Exception err)
share|improve this answer
var userID = items[0].ID; – IAfanasov Apr 25 '12 at 6:21

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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