none
programmatically getting user profile data in sharepoint 2010

    Question

  • How to retrieve user profile details like user name and login name in sp2010

    sree

    Monday, May 21, 2012 1:02 PM

Answers

  • You can use the following code to access another user's user profile details.

    public static void GetUserProfilePropertyElevated(string name)
    {
                string pictureUrl = string.Empty;
                string preferredName = string.Empty;
                string loginName = string.Empty;
    
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite site = new SPSite("http://basesmc2008"))
                    {
                        using (SPWeb web = site.OpenWeb())
                        {
                            SPUser user = web.EnsureUser(name);
                            SPServiceContext context = SPServiceContext.GetContext(site);
                            UserProfileManager upm = new UserProfileManager(context, true);
                                                 
                            UserProfile up = upm.GetUserProfile(user.RawSid);
    
                            pictureUrl = up["PictureURL"].Value != null ? up["PictureUrl"].Value.ToString() : string.Empty;
                            preferredName = up["PreferredName"].Value.ToString();
                            loginName = up["AccountName"].Value.ToString();
                           
                        }
                                 
                    }
                       
                });
    }


    Blog | SharePoint Field Notes Dev Tool | ClassMaster

    Monday, May 21, 2012 4:16 PM
  • Hello,

    You can use the SPFieldUser and SPFieldUserValue class :http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spfielduservalue.aspx

    something like this

     SPFieldUserValue userValue = new SPFieldUserValue(web, item["Author"].ToString());
                                        SPUser user = userValue.User;
                                        string login = user.LoginName;
                                        string name = user.Name;

    Hope this help!s


    Best regards, Christopher.
    Blog | Mail


    Monday, May 21, 2012 1:10 PM
    Answerer

All replies

  • Hello,

    You can use the SPFieldUser and SPFieldUserValue class :http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spfielduservalue.aspx

    something like this

     SPFieldUserValue userValue = new SPFieldUserValue(web, item["Author"].ToString());
                                        SPUser user = userValue.User;
                                        string login = user.LoginName;
                                        string name = user.Name;

    Hope this help!s


    Best regards, Christopher.
    Blog | Mail


    Monday, May 21, 2012 1:10 PM
    Answerer
  • You can use the following code to access another user's user profile details.

    public static void GetUserProfilePropertyElevated(string name)
    {
                string pictureUrl = string.Empty;
                string preferredName = string.Empty;
                string loginName = string.Empty;
    
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite site = new SPSite("http://basesmc2008"))
                    {
                        using (SPWeb web = site.OpenWeb())
                        {
                            SPUser user = web.EnsureUser(name);
                            SPServiceContext context = SPServiceContext.GetContext(site);
                            UserProfileManager upm = new UserProfileManager(context, true);
                                                 
                            UserProfile up = upm.GetUserProfile(user.RawSid);
    
                            pictureUrl = up["PictureURL"].Value != null ? up["PictureUrl"].Value.ToString() : string.Empty;
                            preferredName = up["PreferredName"].Value.ToString();
                            loginName = up["AccountName"].Value.ToString();
                           
                        }
                                 
                    }
                       
                });
    }


    Blog | SharePoint Field Notes Dev Tool | ClassMaster

    Monday, May 21, 2012 4:16 PM