Monday, 15 July 2013

Getting User Profile

For fetching User profile details like an organization chart.
We need to add two dll reference.
1. Microsoft.Office.Server
2. Microsoft.Office.Server.UserProfiles

ASCX Code

<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls"
    Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Register TagPrefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages"
    Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="OrgChartUC.ascx.cs"
    Inherits="OrgChart.VisualWebPart1.VisualWebPart1UserControl" %>
<%@ Register Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    Namespace="System.Web.UI.WebControls" TagPrefix="asp" %>
   
<table border="1" width="100%" >
    <tr>
        <td width="20%" style="font-weight:bold">
            <asp:Label ID="lblManagerHierarcy" runat="server" Text="Manager Hierarcy"></asp:Label>
            <asp:ListView ID="lstViewManagers" runat="server"  GroupPlaceholderID="groupPlaceholder1" ItemPlaceholderID="itemPlaceholder1" >
             <LayoutTemplate>
                    <div id="groupPlaceholder1" runat="server">
                    </div>
                </LayoutTemplate>
                <GroupTemplate>
                    <div id="itemPlaceholder1" runat="server">
                    </div>
                </GroupTemplate>
                <ItemTemplate>
                    <div class="managers_details">
                        <a href='<%# "OCPerson.aspx?UserId=" + Eval("AccountName").ToString()%>'>
                           
                                <asp:Label ID="lblName" runat="server" Text='<%# Eval("PreferredName") %>'></asp:Label>
                        </a>
                    </div>
                </ItemTemplate>
            </asp:ListView>
        </td>
        <td colspan="3" rowspan="6">
            <table width="100%" cellpadding=2 cellspacing=2>
                <tr >
                    <td colspan="3" rowspan=3 height="110px">
                    <asp:Label ID="lblName"  runat="server"></asp:Label><br>
                    <asp:Image ID="imgPicUrl" runat="server" /><br>
                    <asp:Label ID="lblDesgnVal" runat="server"></asp:Label>
                    </td>
                   
                     <td style="font-weight:bold">
                    <asp:Label ID="lblOFfice" Text="Office" runat="server" ></asp:Label>
                    </td>
                    <td>
                    <asp:Label ID="lblOfficeVal"  runat="server" ></asp:Label>
                    </td>


                </tr>
                 <tr>
                    <td style="font-weight:bold">
                        <asp:Label ID="lblInternetId" Text="Internet ID" runat="server"></asp:Label>
                    </td>
                    <td>
                        <asp:Label ID="lblInternetIdVal" runat="server"></asp:Label>
                    </td>
                   
                   
                  
                </tr>
                <tr>
                   <td style="font-weight:bold">
                        <asp:Label ID="lblPh" Text="Phone" runat="server"></asp:Label>
                    </td>
                    <td>
                        <asp:Label ID="lblPhNo" runat="server"></asp:Label>
                    </td>
                   
                    
                </tr>
                <tr>
                    <td style="font-weight:bold">
                        <asp:Label ID="lblEmail" Text="Email" runat="server"></asp:Label>
                    </td>
                    <td colspan="2">
                        <asp:Label ID="lblEMailVal" runat="server"></asp:Label>
                    </td>
                    <td style="font-weight:bold">
                     <asp:Label ID="lblMgr" Text="Manager" runat="server"></asp:Label>
                    </td>
                    <td>
                    <asp:Label ID="lblMgrVal"  runat="server"></asp:Label>
                    </td>
                </tr>
                <tr>
                   <td style="font-weight:bold">
                        <asp:Label ID="lblDesgn" Text="Department" runat="server"></asp:Label>
                    </td>
                     <td colspan="2">
                         <asp:Label ID="lblDeptValue"  runat="server"></asp:Label>
                    </td>
                    <td style="font-weight:bold">
                     <asp:Label ID="lblBusinessLine" Text="Business Line" runat="server"></asp:Label>
                    </td>
                    <td>
                    <asp:Label ID="lblBusinessLineVal"  runat="server"></asp:Label>
                    </td>
                </tr>
                <tr>
                <td style="font-weight:bold">
                     <asp:Label ID="lblPresenceIcon" Text="Assistant" runat="server"></asp:Label>
                    </td>
                     <td colspan="2">
                    <asp:Label ID="lblAssistant"  runat="server"></asp:Label>
                    </td>
                    <td style="font-weight:bold">
                     <asp:Label ID="lblFax" Text="Fax" runat="server"></asp:Label>
                    </td>
                    <td>
                    <asp:Label ID="lblFaxNo"  runat="server"></asp:Label>
                    </td>
                   
                </tr>
                <tr>
                    <td>
                    </td>
                    <td colspan="2">
                    </td>
                    <td>
                    </td>
                    <td>
                    </td>
                   
                </tr>
            </table>
        </td>
    </tr>
    <tr>
        <td style="font-weight:bold">
            <asp:Label ID="lblDirectReports" runat="server"  Text="Direct Reports"></asp:Label>
            <hr />
            <asp:ListView ID="lstViewReportees" runat="server"
             GroupPlaceholderID="groupPlaceholder1" ItemPlaceholderID="itemPlaceholder1" >
             <LayoutTemplate>
                    <div id="groupPlaceholder1" runat="server">
                    </div>
                </LayoutTemplate>
                <GroupTemplate>
                    <div id="itemPlaceholder1" runat="server">
                    </div>
                </GroupTemplate>
                <ItemTemplate>
                    <div class="managers_details">
                         <a href='<%# "OCPerson.aspx?UserId=" + Eval("AccountName").ToString()%>'>
                           
                                <asp:Label ID="lblName" runat="server" Text='<%# Eval("PreferredName") %>'></asp:Label>
                        </a>
                    </div>
                </ItemTemplate>
            </asp:ListView>
        </td>
    </tr>
    <tr>
        <td style="font-weight:bold">
            <asp:Label ID="lblPeers" runat="server" Text="Peers"></asp:Label>
            <hr />
            <asp:ListView ID="lstViewPeers" runat="server"
             GroupPlaceholderID="groupPlaceholder1" ItemPlaceholderID="itemPlaceholder1" >
             <LayoutTemplate>
                    <div id="groupPlaceholder1" runat="server">
                    </div>
                </LayoutTemplate>
                <GroupTemplate>
                    <div id="itemPlaceholder1" runat="server">
                    </div>
                </GroupTemplate>
                <ItemTemplate>
                    <div class="Peers_details">
                         <a href='<%# "OCPerson.aspx?UserId=" + Eval("AccountName").ToString()%>'>
                           
                                <asp:Label ID="lblName" runat="server" Text='<%# Eval("PreferredName") %>'></asp:Label>
                        </a>
                    </div>
                </ItemTemplate>
            </asp:ListView>
        </td>
    </tr>
    <tr>
        <td>
        </td>
    </tr>
    <tr>
        <td>
        </td>
    </tr>
    <tr>
        <td>
        </td>
    </tr>
</table>

Code Behind

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.Office.Server;
using Microsoft.Office.Server.UserProfiles;
using System.Collections.Generic;
using System.Data;

namespace OrgChart.VisualWebPart1
{
    public partial class VisualWebPart1UserControl : UserControl
    {
        List<UserProfile> managers;
        List<UserProfile> peers;
        List<UserProfile> reportee;

        protected void Page_Load(object sender, EventArgs e)
        {
            string userActId = "domain\\User_Id";
            if (Request.QueryString["UserId"] != null)
            {
                userActId = Request.QueryString["UserId"];
               
            }
            else {
                userActId =
"domain\\User_Id";         
   }
            try
            {
                GetUserDetails(userActId);
            }
            catch (Exception ex)
            {
           
            }
        }

        private void GetUserDetails(string AcctId)
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite site = new SPSite(SPContext.Current.Site.ID))
                {
                    UserProfile userProfile = null;
                    SPServiceContext cntx = SPServiceContext.GetContext(site);
                    UserProfileManager userProfileMgr = new UserProfileManager(cntx);

                    userProfile = userProfileMgr.GetUserProfile( AcctId);
                    //ServerContext cntx = ServerContext.GetContext(site);
                    PopulateUserDetails(userProfile);
                   
                    managers = new List<UserProfile>(userProfile.GetManagers());
                    reportee = new List<UserProfile>(userProfile.GetDirectReports());
                    peers = new List<UserProfile>(userProfile.GetPeers());

                    PopulateManagers(managers);
                    PopulateReportees(reportee);
                    PopulatePeers(peers);
                }
            });

        }
        private void PopulateUserDetails(UserProfile userDetails)
        {
            if (userDetails != null)
            {
                imgPicUrl.ImageUrl = userDetails["PictureURL"].Value != null ? userDetails["PictureUrl"].Value.ToString() : string.Empty;
                lblOfficeVal.Text = userDetails["Office"].Value != null ? userDetails["Office"].Value.ToString() : string.Empty;
                lblName.Text = userDetails["PreferredName"].Value != null ? userDetails["PreferredName"].Value.ToString() : string.Empty;
                lblInternetIdVal.Text = userDetails["AccountName"].Value != null ? userDetails["AccountName"].Value.ToString() : string.Empty;
                lblBusinessLineVal.Text = userDetails["SPS-MemberOf"].Value != null ? userDetails["SPS-MemberOf"].Value.ToString() : string.Empty;
                lblDesgnVal.Text = userDetails["Title"].Value != null ? userDetails["Title"].Value.ToString() : string.Empty;
                lblEMailVal.Text = userDetails["WorkEmail"].Value != null ? userDetails["WorkEmail"].Value.ToString() : string.Empty;
                lblFaxNo.Text = userDetails["Fax"].Value != null ? userDetails["Fax"].Value.ToString() : string.Empty;
                lblMgrVal.Text = userDetails["Manager"].Value != null ? GetManagerById(userDetails["Manager"].Value.ToString()) : string.Empty;
                lblPhNo.Text = userDetails["WorkPhone"].Value != null ? userDetails["WorkPhone"].Value.ToString() : string.Empty;
                lblAssistant.Text =  userDetails["Assistant"].Value != null ? userDetails["Assistant"].Value.ToString() : string.Empty;
                lblDeptValue.Text = userDetails["Department"].Value != null ? userDetails["Department"].Value.ToString() : string.Empty;
            }

        }
        private string GetManagerById(string id)
        {
            string mgrName = string.Empty;
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite site = new SPSite(SPContext.Current.Site.ID))
                {
                    UserProfile userProfile = null;
                    SPServiceContext cntx = SPServiceContext.GetContext(site);
                    UserProfileManager userProfileMgr = new UserProfileManager(cntx);
                    userProfile = userProfileMgr.GetUserProfile(id);
                    mgrName = userProfile["PreferredName"].Value != null ? userProfile["PreferredName"].Value.ToString() : string.Empty;
                }
            });
            return mgrName;
        }
        private void PopulateManagers(List<UserProfile> Managers)
        {
            DataTable tblManager = new DataTable();
            tblManager.Columns.Add("PreferredName", typeof(string));
            tblManager.Columns.Add("AccountName", typeof(string));
           
            foreach (UserProfile usrDetail in Managers)
            {
                DataRow dr = tblManager.NewRow();
                dr["PreferredName"] = usrDetail["PreferredName"].Value != null ? usrDetail["PreferredName"].Value.ToString() : string.Empty;
                dr["AccountName"] = usrDetail["AccountName"].Value != null ? usrDetail["AccountName"].Value.ToString() : string.Empty; //usrDetail["AccountName"].Value;
                tblManager.Rows.Add(dr);
            }
            lstViewManagers.DataSource = tblManager;
            lstViewManagers.DataBind();
        }
        private void PopulatePeers(List<UserProfile> Peers)
        {
            DataTable tblPeers = new DataTable();
            tblPeers.Columns.Add("PreferredName", typeof(string));
            tblPeers.Columns.Add("AccountName", typeof(string));

            foreach (UserProfile usrDetail in Peers)
            {
                DataRow dr = tblPeers.NewRow();
                dr["PreferredName"] = usrDetail["PreferredName"].Value != null ? usrDetail["PreferredName"].Value.ToString() : string.Empty;
                dr["AccountName"] = usrDetail["AccountName"].Value != null ? usrDetail["AccountName"].Value.ToString() : string.Empty;
                tblPeers.Rows.Add(dr);
            }
            lstViewPeers.DataSource = tblPeers;
            lstViewPeers.DataBind();
        }
        private void PopulateReportees(List<UserProfile> Reportees)
        {
            DataTable tblReportees = new DataTable();
            tblReportees.Columns.Add("PreferredName", typeof(string));
            tblReportees.Columns.Add("AccountName", typeof(string));

            foreach (UserProfile usrDetail in Reportees)
            {
                DataRow dr = tblReportees.NewRow();
                dr["PreferredName"] = usrDetail["PreferredName"].Value != null ? usrDetail["PreferredName"].Value.ToString() : string.Empty;
                dr["AccountName"] = usrDetail["AccountName"].Value != null ? usrDetail["AccountName"].Value.ToString() : string.Empty;
                tblReportees.Rows.Add(dr);
            }
            lstViewReportees.DataSource = tblReportees;
            lstViewReportees.DataBind();
        }
    }
}