Added references to System.DirectoryServices and System.Security. The display of the hierarchy will be a tree structure. Following is the code snippet:
When the WebPart feature is added to the WSPBuilder project, the webpart cs file is also created. The two important functions are BuildStructure and CreateChildControls.
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.DirectoryServices;
using System.Security;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
namespace WSPCreator
{
[Guid("25b6a3cf-4f71-43ca-bad0-dcb2c35b4152")]
public class UserStructureInOrganization : Microsoft.SharePoint.WebPartPages.WebPart
{
private bool _error = false;
private string _myProperty = null;
[Personalizable(PersonalizationScope.Shared)]
[WebBrowsable(true)]
[System.ComponentModel.Category("My Property Group")]
[WebDisplayName("MyProperty")]
[WebDescription("Meaningless Property")]
public string MyProperty
{
get
{
if (_myProperty == null)
{
_myProperty = "Hello SharePoint";
}
return _myProperty;
}
set { _myProperty = value; }
}
public UserStructureInOrganization()
{
this.ExportMode = WebPartExportMode.All;
}
///
/// Create all your controls here for rendering.
/// Try to avoid using the RenderWebPart() method.
///
protected override void CreateChildControls()
{
base.CreateChildControls();
System.Security.Principal.IPrincipal User;
User = System.Web.HttpContext.Current.User;
string struser = User.Identity.Name;
string strusername = struser.Remove(0, struser.IndexOf("\\") + 1);
TreeView tvwOrg1 = new TreeView();
tvwOrg1.ID = "OrganizationStructure";
tvwOrg1.Width = Unit.Pixel(160);
tvwOrg1.Height = Unit.Pixel(250);
//strusername = "steeve";
BuildStructure(strusername, tvwOrg1);
this.Controls.Add(tvwOrg1);
}
private void BuildStructure(string strusr, TreeView tvwOrg)
{
DirectoryEntry DET = new DirectoryEntry("LDAP://ADServer");
DirectorySearcher DS = new DirectorySearcher(DET);
SearchResult RS;
DS.PropertiesToLoad.Add("manager");
DS.PropertiesToLoad.Add("directReports"); //People who report to steeve//
DS.Filter = ("(&(objectCategory=person)(objectClass=user)(samaccountname=" + strusr + "))");
//Only one manager for this object needs to be found//
RS = DS.FindOne();
DirectoryEntry Mgr = new DirectoryEntry("LDAP://" + RS.Properties["manager"][0].ToString());
//MessageBox.Show(Mgr.Properties["DisplayName"].Value.ToString());
string strUserMgr = Mgr.Properties["DisplayName"].Value.ToString() + " (Reporting Manager)";
TreeNode TN = new TreeNode(strUserMgr);
tvwOrg.Nodes.Add(TN);
TreeNode TNUser = new TreeNode(strusr);
TN.ChildNodes.Add(TNUser);
///end of part 1 gets the manager of the user//
//Gets directreportees for the user//
foreach (SearchResult RSUnderUser1 in DS.FindAll())
{
int icntchild1 = RSUnderUser1.Properties["directReports"].Count;
for (int IIchild1 = 0; IIchild1 < icntchild1; IIchild1++)
{
DirectoryEntry DE1 = new DirectoryEntry("LDAP://" + RSUnderUser1.Properties["directReports"][IIchild1].ToString());
if (DE1.Properties["DisplayName"].Value.ToString() != "")
{
TreeNode subTN = new TreeNode(DE1.Properties["DisplayName"].Value.ToString() + " (Team Member)");
TNUser.ChildNodes.Add(subTN);
}
}
}
/////////////////////////////////////////////////
//Start to get steeves sibblings// part - 3
DirectorySearcher DSMgr = new DirectorySearcher(Mgr);
//Add directreports//
DSMgr.PropertiesToLoad.Add("directReports");
DSMgr.PropertiesToLoad.Add("userPrincipalName");
DSMgr.Filter = ("(&(objectCategory=person)(objectClass=user)(samaccountname=" + strUserMgr + "))");
foreach (SearchResult RSUnderMgr in DSMgr.FindAll())
{
int icntchild = RSUnderMgr.Properties["directReports"].Count;
for (int IIchild = 0; IIchild < icntchild; IIchild++)
{
DirectoryEntry DE2 = new DirectoryEntry("LDAP://" + RSUnderMgr.Properties["directReports"][IIchild].ToString());
}
}
tvwOrg.ShowExpandCollapse = false;
tvwOrg.ExpandAll();
///////////////////////////////////////////////////////
}
///
/// Ensures that the CreateChildControls() is called before events.
/// Use CreateChildControls() to create your controls.
///
///
protected override void OnLoad(EventArgs e)
{
if (!_error)
{
try
{
base.OnLoad(e);
this.EnsureChildControls();
// Your code here...
}
catch (Exception ex)
{
HandleException(ex);
}
}
}
///
/// Clear all child controls and add an error message for display.
///
///
private void HandleException(Exception ex)
{
this._error = true;
this.Controls.Clear();
this.Controls.Add(new LiteralControl(ex.Message));
}
}
}
The advantage of using the WSPbuilder project is that it gives the WSP solution and also the bat files for deploying the WSP solution on the sharepoint server.
