Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

Hi I developed a TreeView that listing groups and users in them, but for some reason I cant find the best way even googled it,

here's the Code

protected override void CreateChildControls()
    {
        base.CreateChildControls();

        try
        {
            tree = new TreeView();


            foreach (SPUser User in SPContext.Current.Web.Users)
            {
                string GroupName = FormatUserLogin(User.Name);

                if (User.IsDomainGroup && GroupName != "" && DoesGroupExist(GroupName))
                {
                    TreeNode GroupNode = new TreeNode(GroupName);
                    List<ADUser> ADUsers = new List<ADUser>();

                    ADUsers = GetUsersFromGroup(GroupName);

                    foreach (ADUser member in ADUsers)
                    {
                        if (member != null && member.DisplayName != "")
                        {
                            TreeNode MemberName = new TreeNode(member.DisplayName);
                            GroupNode.ChildNodes.Add(MemberName);
                        }
                    }
                }
            }



        }
        catch (Exception ex)
        {
            //loggingit
        }
    }

I tried adding control but still no gain

Cheers

share|improve this question

2 Answers 2

up vote 2 down vote accepted

Try this code. Besides it's not recommended to start a local variable with an UpperCase.

protected override void CreateChildControls()
{
    base.CreateChildControls();

    try
    {
        tree = new TreeView();

        foreach (SPUser user in SPContext.Current.Web.Users)
        {
            string groupName = FormatUserLogin(user.Name);

            if (user.IsDomainGroup && !string.IsNullOrEmpty(groupName) && DoesGroupExist(groupName))
            {
                TreeNode groupNode = new TreeNode(groupName);
                List<ADUser> adUsers = GetUsersFromGroup(groupName);

                foreach (ADUser member in adUsers)
                {
                    if (member != null && !string.IsNullOrEmpty(member.DisplayName))
                    {
                        TreeNode memberNode = new TreeNode(member.DisplayName);
                        groupNode.Nodes.Add(memberNode);
                    }
                }

                if(groupNode.Nodes.Count > 0)
                {
                    tree.Nodes.Add(groupNode);
                }
            }
        }

        this.Controls.Add(tree);
    }
    catch (Exception ex)
    {
        //loggingit
    }
}
share|improve this answer

Right before your catch you need to add the TreeView control to the page.

        this.Controls.Add(tree);
    }
    catch (Exception ex)
    {
        //loggingit
    }
}
share|improve this answer
2  
In addition to your comment TimeToThine also needs to add TreeNodes to the tree object. –  Farrell May 9 '12 at 15:11
    
thanks guys, how I gonna do tht Farrel @adding TreeNodes to tree object –  Muhammad Raja May 9 '12 at 15:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.