Home > SharePoint Development > Get user information from Sharepoint User Profile in Sharepoint using C#

Get user information from Sharepoint User Profile in Sharepoint using C#

using (SPSite site = new SPSite("http://localhost:port"))
{
ServerContext context = ServerContext.GetContext(site);
UserProfileManager profileManager = new UserProfileManager(context);
UserProfile myProfile = profileManager.GetUserProfile("domian\username");
strDepartment = Convert.ToString(myProfile[PropertyConstants.Department].Value);
strJobTitle = Convert.ToString(myProfile[PropertyConstants.Title].Value);
}
Advertisements
  1. nguyen van
    11/08/2011 at 1:42 pm

    I am thanks you for your code,
    I am write a function
    private void GetUserProfile()
    {
    string siteURL = SPContext.Current.Site.Url;
    SPSecurity.RunWithElevatedPrivileges(delegate()
    {
    using (SPSite site = new SPSite(siteURL))
    {
    ServerContext context = ServerContext.GetContext(site);
    UserProfileManager profileManager = new UserProfileManager(context);
    SPUser user = SPContext.Current.Web.CurrentUser;
    UserProfile myProfile = profileManager.GetUserProfile(user.LoginName);
    string strDepartment = string.Empty;
    string strJobTitle = string.Empty;
    strDepartment = Convert.ToString(myProfile[PropertyConstants.Department].Value);
    strJobTitle = Convert.ToString(myProfile[PropertyConstants.Title].Value);
    }
    });
    }
    But I don’t know, why strDepartment and strJobTitle alway return null value.I am sure that “Department and Title” is not null.
    Would you help me,

    • 12/08/2011 at 11:57 am

      Hi,

      There may be two reason.
      1. myProfile object is retuning null value.
      2. Department or Title may be empty in the sharepoint profile.

      Can you check sharepoint profile with same user and see if there is any value in the title and department

  2. nguyen van
    15/08/2011 at 9:04 am

    Dear Sanjiv
    1,I have been check Sharepoint profile with the same name and i see value title equal “Supporter”, Department equal “ICT”, is mean not null,
    2, when i debug i see:
    m_UserProfileFields collection count=12,
    [0]: has key “Accountname” and Value “North\vanCuong”
    [1]: has Key “Preferredname” and value “North\vanCuong”
    [2]: has key “WorkEmail” and value “Null”

    [6] has Key “Title” and value “Null”
    [7] has Key “Department” and value “Null”
    ……
    [11] has Key “PersionalSpace” and value “Null”
    The Above is mean myProfile object is not null.But i don’t know why “Title”,”Department” is null, Although I have been check Sharepoint profile with the same name and i see value title equal “Supporter”, Department equal “ICT”

    Would you help me!

  3. Piyush mishra
    24/05/2013 at 3:26 pm

    what a solution……i ma glad to see it

    • Uthaya
      29/04/2014 at 12:49 pm

      Please remove the SPSecurity.RunWithElevatedPrivileges(delegate()
      { }); it will work

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

%d bloggers like this: