I need to create a web part for SharePoint that reads a list and creates a tree view.
The tree view should be organized as follows: The root (or roots) should be created by the selection field that represents the category, for example, Drinks , child nodes are the name of the rows that contain this category, the tree view must be created programmatically.
List:
Title(string) Category(Choice)
Coke Drinks
Beer Drinks
Fish Food
Chips Food
Would produce this:
Drinks
Coke
Beer
Food
Fish
Chips
code i still
TreeView treeView;
TreeNode rootNode;
TreeNode childNode;
protected override void RenderContents(System.Web.UI.HtmlTextWriter writer)
{
base.RenderContents(writer);
}
protected override void CreateChildControls()
{
List<TreeNode> items = new List<TreeNode>();
base.CreateChildControls();
using (SPSite Site = new SPSite(SPContext.Current.Site.Url + "/UberWiki"))
{
using (SPWeb currentWeb = Site.OpenWeb())
{
SPList list = currentWeb.Lists["Pages"];
SPFieldChoice field = (SPFieldChoice)list.Fields["Categories"];
foreach (string str in field.Choices)
{
treeView = new System.Web.UI.WebControls.TreeView();
rootNode = new System.Web.UI.WebControls.TreeNode(str);
treeView.Nodes.Add(rootNode);
foreach (SPListItem rows in list.Items)
{
childNode = new System.Web.UI.WebControls.TreeNode(rows.Title);
treeView.Nodes.Add(childNode);
}
}
}
this.Controls.Add(treeView);
base.CreateChildControls();
}
}
source
share