C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » Active Directory »

Active Directory in Treeview in ASp.net with CheckBox


Posted Date: 12 May 2009    Resource Type: Code Snippets    Category: Active Directory
Author: Gnana Prakash Member Level: Gold    
Rating: 1 out of 5Points: 15



This is an example of how to build an ASP.Net LDAP

The Sample project is having all the Functionality like load all the directory details to Treeview on pageload and also dynamically load LADP using user given details like LADP Path,username,passowrd.

below I am going to give some important Functionality only. so download project and Go through all the Functionality .


variable declaraiton


hdnAdPath.Value = ConfigurationManager.AppSettings["AdPathRoot"].ToString();
hdnUserName.Value = ConfigurationManager.AppSettings["UserName"].ToString();
hdnUserPassword.Value = ConfigurationManager.AppSettings["Password"].ToString();
tvDomainController.Visible = false;
lblStatus.Visible = false;




First Step 1:

In Page load event check whether system in domain control or not



protected void CheckSystemLevel()
{

// Get the Domain Name
String DomainName = Environment.UserDomainName.ToString();


// Get Domain Server name
String domainserverName = Environment.GetEnvironmentVariable("LOGONSERVER");

// Get Local System Name
string localSystemName = Environment.MachineName.ToString();


// Check whether system in domain control or not
if (!domainserverName.Contains(localSystemName) && DomainName != localSystemName)
{
// Client Machine
// System is connection to Domain Server
hdnStatus.Value = "false";
LoadDomainName();
}
else if (DomainName == localSystemName && domainserverName.Contains(localSystemName))
{

// Local System ???
// System is not having domaincontroller
hdnStatus.Value= "true";
LoadDomainDetailswithPath();


}

else if (domainserverName.Contains(localSystemName) && DomainName != localSystemName)
{

// Domain Controller System
// server System
hdnStatus.Value = "false";
LoadDomainName();

}



}




Step 2:

Load Root node in tree view



protected void LoadDomainName()
{
try
{
DirectoryEntry adRootDSE = new DirectoryEntry(_adrootPath);
DirectoryEntry adRoot = new DirectoryEntry("LDAP://" + (string)adRootDSE.Properties["defaultNamingContext"].Value);

TreeNode tnRoot = new TreeNode();

tnRoot.Text = adRoot.Properties["Name"].Value.ToString();
tnRoot.Value = adRoot.Path.ToString().Replace("/", "\\");
tnRoot.SelectAction = TreeNodeSelectAction.Expand;
tnRoot.PopulateOnDemand = true;
tnRoot.ShowCheckBox = false;
tnRoot.ImageUrl = adImageURL[(int)AdImages.AdRoot];
tvDomainController.Nodes.Clear();
tvDomainController.Nodes.Add(tnRoot);
tvDomainController.Visible = true;
lblStatus.Visible = false;
}
catch (Exception ex)
{
lblStatus.Text = "Directory expecting username and password";
lblStatus.Visible = true;
tvDomainController.Visible = false;
}
}




STEP 3:

Load all the directorys in treeview using following code


protected void PopulateNode(Object source, TreeNodeEventArgs e)
{

if (e.Node.Value != string.Empty)
{
DirectoryEntry selectedEntry;

if (hdnStatus.Value == "false" )
{
selectedEntry = new DirectoryEntry(e.Node.Value.Replace("\\", "/"));
}
else
{
selectedEntry = new DirectoryEntry(e.Node.Value.Replace("\\", "/"), hdnUserName.Value, hdnUserPassword.Value);
}



if (DirectoryEntry.Exists(selectedEntry.Path))
{

foreach (DirectoryEntry child in selectedEntry.Children)
{
TreeNode tvNode = e.Node;
TreeNode tmpNode = new TreeNode();


switch (child.SchemaClassName)
{
case "organizationalUnit":
tmpNode.Text = child.Properties["Name"].Value.ToString();
tmpNode.Value = child.Path.ToString().Replace("/", "\\");
tmpNode.SelectAction = TreeNodeSelectAction.Expand;
tmpNode.PopulateOnDemand = true;
tmpNode.ImageUrl = adImageURL[(int)AdImages.Ou];

tvNode.ChildNodes.Add(tmpNode);
break;

case "container":

tmpNode.Text = child.Properties["Name"].Value.ToString();
tmpNode.Value = child.Path.ToString().Replace("/", "\\");
tmpNode.SelectAction = TreeNodeSelectAction.Expand;
tmpNode.PopulateOnDemand = true;
tmpNode.ImageUrl = adImageURL[(int)AdImages.Container];
tvNode.ChildNodes.Add(tmpNode);
break;

case "computer":
tmpNode.Text = child.Properties["Name"].Value.ToString() + child.SchemaClassName.ToString();
tmpNode.Value = child.Path.ToString().Replace("/", "\\");
tmpNode.SelectAction = TreeNodeSelectAction.Expand;
tmpNode.PopulateOnDemand = true;
tmpNode.ImageUrl = adImageURL[(int)AdImages.Computer];
tvNode.ChildNodes.Add(tmpNode);
break;

case "user":
tmpNode.Text = child.Properties["Name"].Value.ToString();
tmpNode.Value = child.Path.ToString().Replace("/", "\\");
tmpNode.SelectAction = TreeNodeSelectAction.None;
tmpNode.PopulateOnDemand = false;
tmpNode.ImageUrl = adImageURL[(int)AdImages.User];
tvNode.ChildNodes.Add(tmpNode);
break;

case "group":
tmpNode.Text = child.Properties["Name"].Value.ToString();
tmpNode.Value = child.Path.ToString().Replace("/", "\\");
tmpNode.SelectAction = TreeNodeSelectAction.None;
tmpNode.PopulateOnDemand = false;
tmpNode.ImageUrl = adImageURL[(int)AdImages.Group];
tvNode.ChildNodes.Add(tmpNode);
break;

default:
tmpNode.Text = child.Properties["Name"].Value.ToString() + child.SchemaClassName.ToString();
tmpNode.Value = child.Path.ToString().Replace("/", "\\");
tmpNode.SelectAction = TreeNodeSelectAction.Expand;
tmpNode.PopulateOnDemand = true;
tmpNode.ImageUrl = adImageURL[(int)AdImages.Unknown];
tvNode.ChildNodes.Add(tmpNode);
break;
}
}
}

}



}



Step 4

Find the node using Value path




// Find the node specified by the user.
TreeNode node = tvDomainController.FindNode(Server.HtmlEncode(txtPath.Text));

if (node != null)
{
// Indicate that the node was found.
Message.Text = "The specified node (" + node.ValuePath + ") was found.";
node.Checked = true;
}
else
{
// Indicate that the node is not in the TreeView control.
Message.Text = "The specified node (" + txtPath.Text + ") is not in this TreeView control.";
}



Attachments

  • SampleProject (28378-12444-ActiveDirectory.zip)


  • Responses


    No responses found. Be the first to respond and make money from revenue sharing program.

    Feedbacks      
    Popular Tags   What are tags ?   Search Tags  
    Sign In to add tags.
    LDAP in Treeview  .  LADP in Asp.net  .  Check / uncheck in treeview  .  Active directory in Treeview with checkbox  .  

    Post Feedback


    This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
    You must Sign In to post a response.
    Next Resource: Querying Information for All Drives on a System
    Previous Resource: Search File in a Directory
    Return to Discussion Resource Index
    Post New Resource
    Category: Active Directory


    Post resources and earn money!
     
    More Resources



    dotNet Slackers

    About Us    Contact Us    Privacy Policy    Terms Of Use