none
How to get SPUser from Display Name with C# code? RRS feed

  • Question

  • Hi, I use SharePoint 2010.

    In my event receiver I need to find the SPUser object by the display name like John Smith. (We have unique display names which is good).

    Will greatly appreciate if you could please share the C# code.

    Thanks folks much appreciated.

     

    Thursday, April 18, 2013 12:18 AM

Answers

  • Thanks all, I ended up using this code:

    SPPrincipalInfo pinfo = SPUtility.ResolvePrincipal(web, "John Smith", SPPrincipalType.User, SPPrincipalSource.All, web.Users, false); SPUser user = web.Users[pinfo.LoginName];

    Cheers.

    • Marked as answer by ran009 Thursday, April 18, 2013 5:03 PM
    Thursday, April 18, 2013 5:03 PM

All replies

  • Have you tried

    SPUser newUser = SPContext.Current.Web.EnsureUser(@"domain\logonname");


    Blog | SharePoint Field Notes Dev Tool | ClassMaster

    Thursday, April 18, 2013 2:03 AM
  • Hi,

    I guess if you really want to get User when just using the DisplayName i think the best way is something like this

    SPUser user = (from SPUser c in web.Users
                   where c.Name == "whateverDisplayName"
                   select c).First();

    though it really is faster using the LoginName rather than this displayName


    Let me know if this helps, Ranjoe

    Thursday, April 18, 2013 2:53 AM
  • If you are not sure whether the user(john smith) will be available in your site
     userinfolist.  You cannot get result object from web.users collection.

    You got  to use ensureuser no other way. But it makes the code really slow if
    you are using it by display name. I ran into similar kind of issue earlier.


    Thanks


    Thursday, April 18, 2013 5:49 AM
  • Here , i tested the code with AD user first name and last name : John Smith. This will be the display name.

     public override void FeatureActivated(SPFeatureReceiverProperties properties)
            {
                SPUser user = FindUser("John Smith");
            }

            private SPUser FindUser(string displayName)
            {
                SPUserCollection siteUsers = SPContext.Current.Web.SiteUsers;

                foreach (SPUser user in siteUsers)
                {
                    if (user.Name == displayName)
                        return user;
                }

                return null;
            }

    Thursday, April 18, 2013 6:26 AM
  • Thanks all, I ended up using this code:

    SPPrincipalInfo pinfo = SPUtility.ResolvePrincipal(web, "John Smith", SPPrincipalType.User, SPPrincipalSource.All, web.Users, false); SPUser user = web.Users[pinfo.LoginName];

    Cheers.

    • Marked as answer by ran009 Thursday, April 18, 2013 5:03 PM
    Thursday, April 18, 2013 5:03 PM
  • SPPrincipalInfo pinfo = SPUtility.ResolvePrincipal(web, "John Smith"SPPrincipalType.User,SPPrincipalSource.All, web.AllUsers, false); SPUser user = web.Users[pinfo.LoginName];
    Tuesday, September 1, 2015 11:47 AM
  • Kalimidi, thanks for adding absolutely nothing by copying the accepted answer, and proposing it as the correct answer *facepalm.
    Thursday, November 17, 2016 9:58 PM