Search This Blog

Tuesday 19 November 2013

Get User Profile Information in SharePoint 2013



To retrieve user profile information in SharePoint 2013, we need to add below references in out SharePoint Project.

           1.       Microsoft.Office.Server
           2.       Microsoft.Office.Server.UserProfiles

Refer below code snippets for reference

Code behind :
private void GetUserProfileInfo()
        {
            try
            {
                UserProfileManager usrProfileMgr = new UserProfileManager(SPServiceContext.GetContext(SPContext.Current.Site));
                UserProfile usrProfile = usrProfileMgr.GetUserProfile(SPContext.Current.Web.CurrentUser.LoginName);
                if (usrProfile != null)
                {
                    lblName.Text = usrProfile.DisplayName;
                    string origUrl = (string)usrProfile[PropertyConstants.PictureUrl].Value;
                    if (!string.IsNullOrEmpty(origUrl))
                    {
                        imgUser.ImageUrl = origUrl;
                    }
                    else
                    {
                        imgUser.ImageUrl = "/_layouts/15/images/PersonPlaceholder.96x96x32.png";
                    }
                    lblDesignation.Text = Convert.ToString(usrProfile[PropertyConstants.JobTitle].Value);
                    lblDepartment.Text = Convert.ToString(usrProfile[PropertyConstants.Department].Value);
                    lblEmail.Text = Convert.ToString(usrProfile[PropertyConstants.WorkEmail].Value);
                }
            }
            catch (Exception ex)
            {
                lblError.Text = ex.Message;
            }
        }
Here, GetUserProfileInfo() method we have called in Page_Load
protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                GetUserProfileInfo();
            }
        }
 
Ascx 

<table cellpadding="5px" cellspacing="0">
    <tr>
        <td>
            <asp:Image ID="imgUser" runat="server" Height="50px" Width="50px" />
        </td>
        <td>
            <asp:Label ID="lblName" runat="server"></asp:Label>
            <br />
            <asp:Label ID="lblDesignation" runat="server"></asp:Label>
            <br />
            <asp:Label ID="lblDepartment" runat="server"></asp:Label>
            <br />
            <asp:Label ID="lblEmail" runat="server"></asp:Label>
        </td>
    </tr>
    <tr>
        <td colspan="2">
            <asp:Label ID="lblError" runat="server"></asp:Label>
        </td>
    </tr>
</table>

Result:

More Reference:
Link 1
Link 2
Link 3

No comments:

Post a Comment