Tuesday, May 18, 2010

Custom organization view in sharepoint

We can customize the famous Organization View web part (which is exists on My Site) on our site for our organization!! We can fetch all the related data through Sharepoint Pofile (which is sinks with Active Directory).
Below is the code,

#region Directives
using System;
using System.ComponentModel;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Workflow;
using Microsoft.SharePoint.WorkflowUtil;
using Microsoft.SharePoint.Administration;
using Microsoft.Office.Server;
using Microsoft.SharePoint.Portal;
using Microsoft.Office.Server.UserProfiles;
#endregion

#region Organization
namespace Organization
{


public class OrganizationalView : System.Web.UI.Page
{

#region Global Variables

SPWeb oWeb = null;
protected SPGridView spGridView = null;
protected GridView grdOrganize = null;
protected HyperLink hprLnkManager = null;
public Label lblEmpName = null;
public Label lblManagerDesignation = null;
public Label lblEmpDesignation = null;
protected Label lblSelectedUser = null;
public Label lblEmpJobTitle = null;
public Label lblEmpEmail = null;
public Image imgdown = null;
public Image imgup = null;
# endregion

#region Methods

void GetUserInfo()
{
imgup.ImageUrl = oWeb.Url.ToString() + @"/_layouts/images/reportsup.gif";
imgdown.ImageUrl = oWeb.Url.ToString() + @"/_layouts/images/reportsdown.gif";
SPWebApplication spWebApp = null;
string sAccount = "";
try
{

if (Request.QueryString["ID"] != null)
{
sAccount = Request.QueryString["ID"].ToString();
}
else
{
sAccount = oWeb.CurrentUser.LoginName;
}
// Response.Write(sAccount);
SPSecurity.RunWithElevatedPrivileges(delegate()
{

spWebApp = oWeb.Site.WebApplication;
spWebApp.FormDigestSettings.Enabled = false;
ServerContext context = ServerContext.GetContext(oWeb.Site);
if (HttpContext.Current != null)
{
if (HttpContext.Current.Items["HttpHandlerSPWeb"] == null)
HttpContext.Current.Items["HttpHandlerSPWeb"] = oWeb;
if (HttpContext.Current.Items["Microsoft.Office.ServerContext"] == null)
HttpContext.Current.Items["Microsoft.Office.ServerContext"] = context;
}
UserProfileManager profileManager = new UserProfileManager(context);

UserProfile userProfile = profileManager.GetUserProfile(sAccount);

try
{
lblEmpName.Text = Convert.ToString(userProfile["PreferredName"]);
}
catch { }


try
{ lblEmpJobTitle.Text = Convert.ToString(userProfile["Title"]); }
catch { }
try
{ lblEmpEmail.Text = Convert.ToString(userProfile["WorkEmail"]); }
//txtEmpDivision.Text = Convert.ToString(userProfile["Department"]);
catch { }
/*
DataSet ds = userProfile.Colleagues.GetDataSet();

grdOrganize.DataSource = ds.Tables[0];
grdOrganize.DataBind();
*/
lblSelectedUser.Text = userProfile["PreferredName"].ToString();
try
{ lblEmpDesignation.Text = ", " + Convert.ToString(userProfile["Title"]); }
//txtEmpDivision.Text = Convert.ToString(userProfile["Department"]);
catch { }

DataRow dr = null;
try
{
UserProfile ManagerProfile = profileManager.GetUserProfile(userProfile["Manager"].ToString());
hprLnkManager.NavigateUrl = "OrganizationalView.aspx?ID=" + userProfile["Manager"].ToString();
hprLnkManager.Text = ManagerProfile["PreferredName"].ToString();
imgup.Visible = true;
try
{ lblManagerDesignation.Text = ", " + Convert.ToString(ManagerProfile["Title"]); }
//txtEmpDivision.Text = Convert.ToString(userProfile["Department"]);
catch { }
}
catch
{ }



UserProfile[] dsUF = userProfile.GetDirectReports();
DataTable dt = new DataTable();
dt.Columns.Add("PreferredName");
dt.Columns.Add("LoginName");
dt.Columns.Add("EmployeeDesignation");


if (dsUF.Length == 0)
{
lblSelectedUser.Text = "    " + userProfile["PreferredName"].ToString();
}
else
{
imgdown.Visible = true;
}

for (int i = 0; i < dsUF.Length; i++)
{
dr = dt.NewRow();



dr["LoginName"] = dsUF[i]["AccountName"].ToString();
try
{
dr["EmployeeDesignation"] = dsUF[i]["Title"].ToString();

}
catch
{
dr["EmployeeDesignation"] = "";

}
if (dr["EmployeeDesignation"].ToString() != "")
dr["PreferredName"] = dsUF[i]["PreferredName"].ToString() + ", ";
else
dr["PreferredName"] = dsUF[i]["PreferredName"].ToString();
dt.Rows.Add(dr);

}
grdOrganize.DataSource = dt;
grdOrganize.DataBind();

});

}
catch (Exception ex)
{
Response.Write(ex.Message);
}

}

#endregion

#region Events
protected void Page_Load(object sender, EventArgs e)
{
imgdown.Visible = false;
imgup.Visible = false;
GetUserInfo();
}
#endregion
}
}

#endregion


also i can give you the design code :)

Page MasterPageFile="~masterurl/default.master" Debug="true" Inherits="Organization.OrganizationalView, Organization, Version=1.0.0.0, Culture=neutral, PublicKeyToken=5b8788fec1ddaa1d"
meta:progid="SharePoint.WebPartPages.Document" Language="C#"
Register Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"
Namespace="Microsoft.SharePoint.WebControls" TagPrefix="cc1"


asp:Content ID="Content1" ContentPlaceHolderID="PlaceHolderPageTitle" runat="server" Orgnaizational View
asp:Content
asp:Content ID="Content2" ContentPlaceHolderID="PlaceHolderMain" runat="server" table width="100%" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF"
tr
td style="width: 100%;" colspan="4"
div
span font size="4" strong
asp:Label ID="lblEmpName" runat="server"
asp:Label strong font span
div
div
span font size="1"
asp:Label ID="lblEmpJobTitle" runat="server" asp:Label font span div
div
span font size="1"
asp:Label ID="lblEmpEmail" runat="server"
asp:Label font span div
td
tr
tr
td
br
td valign="top"
table
tr
td colspan="2"
font size="2" b Organization Hierarchy b font
hr
td
tr
tr
td colspan="2"
font size="2" b
asp:Image runat="server" ID="imgup" ImageUrl="layouts/images/reportsup.gif"
asp:HyperLink ID="hprLnkManager" runat="server"> b font asp:Label Font-Size="7" runat="server" ID="lblManagerDesignation" asp:Label td tr
tr
td bgcolor="#dee8ed" colspan="2"
font size="2" b
asp:Image runat="server" ID="imgdown" ImageUrl="layouts/images/reportsdown.gif"
asp:Label ID="lblSelectedUser" runat="server" Hierarchy Ends asp:Label b font
asp:Label Font-Size="7" runat="server" ID="lblEmpDesignation" asp:Label
td
tr

tr
td width="10px"    
td
td
font size="2"
asp:GridView ShowHeader="false" HeaderStyle-CssClass="ms-menutoolbar" RowStyle-CssClass="ms-cal-workitem2"
AutoGenerateColumns="False" HeaderStyle-HorizontalAlign="Left" ID="grdOrganize"
runat="server" CellPadding="3" BackColor="White" BorderColor="#999999"
GridLines="None" BorderStyle="None" BorderWidth="1px" Width="100%">
FooterStyle BackColor="#CCCCCC" ForeColor="Black"
PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center"
Columns
asp:HyperLinkField ItemStyle-Font-Size="10" HeaderText=" Organization Hierarchy" DataTextField="PreferredName"
DataNavigateUrlFields="LoginName" DataNavigateUrlFormatString="OrganizationalView.aspx?ID={0}"
asp:HyperLinkField
asp:TemplateField
ItemTemplate
asp:Label Font-Size="7" ID="lblEmpDesignation" text='<%# Eval("EmployeeDesignation") %>' runat="server" asp:Label
ItemTemplate
asp:TemplateField
Columns
asp:GridView
font
td
tr

table


td
tr
table
asp:Content