Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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;
try
{
  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

4 Answers 4

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
    }
    else{
        // The User doesn't exists
    }
}
share|improve this answer

I offer another practice - query UsesInfoList:

                            using (var site = p)
                            {
                                using (var root = site.RootWeb)
                                {
                                    var query = new SPQuery();
                                    query.ExpandRecurrence=false;
                                    query.ExpandUserField = false;
                                    query.Query =string.Format(
                                        @"<Where><Eq><FieldRef Name=""Name"" /><Value Type=""Text"">{0}</Value></Eq></Where>", 
                                        newLogin);
                                    var items = root.SiteUserInfoList.GetItems(query);
                                    if (items == null || items.Count == 0)
                                    {
                                        continue;
                                    }
                                    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;
                                    user.Update();
                                }
                            }
                        }
                        catch (Exception err)
                        {
                            Log.Error(err);
                            buf.AppendLine(err.ToString());
                        }
share|improve this answer
    
var userID = items[0].ID; –  IAfanasov Apr 25 '12 at 6:21

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

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

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spusercollection.getbyemail.aspx

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
2  
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

Your Answer

 
discard

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.