なぜこの愚かな、またはより意味のないエラーが発生するのかわかりません。通常、文字列を treeNode に追加しますが、このコードで追加しないのはなぜですか?
groupNode.ChildNodes.Add(UserPair.Value);
(なぜだめですか ?)
protected override void CreateChildControls()
{
base.CreateChildControls();
try
{
int Index = 0;
TreeView tree = new TreeView();
TreeNode groupNode;
Dictionary<int, string> GroupList = new Dictionary<int, string>();
Dictionary<int, string> UserList = new Dictionary<int, string>();
List<string> IndividualUserList = new List<string>();
foreach (SPUser user in SPContext.Current.Web.Users)
{
string groupName = FormatUserLogin(user.Name);
if (groupName != "" && groupName != "System Account")
IndividualUserList.Add(groupName);
else if (user.IsDomainGroup && !string.IsNullOrEmpty(groupName) &&
Directory.DoesGroupExist(groupName))
{
Index++;
GroupList.Add(Index, groupName);
List<ADUser> adUsers = Directory.GetUsersFromGroup(groupName);
foreach (ADUser member in adUsers)
{
if (member != null && !string.IsNullOrEmpty(member.DisplayName))
UserList.Add(Index, member.DisplayName);
}
}
}
IndividualUserList.Sort();
foreach (string Item in IndividualUserList)
{
groupNode = new TreeNode(Item);
}
foreach (KeyValuePair<int, string> GroupPair in GroupList)
{
groupNode = new TreeNode(GroupPair.Value);
foreach (KeyValuePair<int, string> UserPair in UserList)
{
if (UserPair.Key == GroupPair.Key)
groupNode.ChildNodes.Add(UserPair.Value);
}
}
tree.Nodes.Add(groupNode);
this.Controls.Add(tree);
}
catch (Exception)
{
//loggingit
}
}
また、コードに論理的な間違いやバグがある場合はお知らせください。
乾杯
答え
if (UserPair.Key == GroupPair.Key)
{
TreeNode userNode = new TreeNode(UserPair.Value);
groupNode.ChildNodes.Add(userNode);
}