Build Tree View Structure for SharePoint List Data
博客地址 http://blog.csdn.net/foxdave
此文参考自->原文链接
版权归原作者所有,我只是进行一下大致的翻译
应坛友要求,帮助验证一下功能。
SharePoint列表数据的树视图结构
1. 创建一个自定义列表,名字就还叫“CategoryDetails”这个吧,我喜欢用规范的英语。
2. 创建两个字段“Category Name”和“Parent Category”,在这里实际上就是ID和ParentID的概念,一个树,自然要有这种层级结构。
第一个字段为文本类型,类似PK的概念,必填且唯一
第二个字段为查阅项,自连接,如上所述。
字段创建完之后我们做一些测试的数据。
4. 部署WebPart解决方案,稍后附上树控件后台代码,原网站上有下载链接,直接下载下来,指定Site URL,修改后部署即可。
在页面上添加该WebPart,显示树状视图
坛友说的不好用实际上是因为字段的名称导致的,在创建字段的时候有空格,那么在用Query查询的时候,需要把空格替换为_x0020_。
后台代码如下
using System; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using Microsoft.SharePoint; using System.Text; using System.Collections.Generic; using System.Web; namespace TreeView.TreeStructure { public partial class TreeStructureUserControl : UserControl { public const string DYNAMIC_CAML_QUERY = "<Where><IsNull><FieldRef Name='{0}' /></IsNull></Where>"; public const string DYNAMIC_CAML_QUERY_GET_CHILD_NODE = "<Where><Eq><FieldRef Name='{0}' /><Value Type='LookupMulti'>{1}</Value></Eq></Where>"; protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { BuildTree(); } } protected void BuildTree() { SPList TasksList; SPQuery objSPQuery; StringBuilder Query = new StringBuilder(); SPListItemCollection objItems; string DisplayColumn = string.Empty; string Title = string.Empty; string[] valueArray = null; treeViewCategories.Nodes.Clear(); try { using (SPSite site = new SPSite(SPContext.Current.Web.Url)) { using (SPWeb web = site.OpenWeb()) { TasksList = SPContext.Current.Web.Lists["CategoryDetails"]; if (TasksList != null) { objSPQuery = new SPQuery(); Query.Append(String.Format(DYNAMIC_CAML_QUERY, "Parent_x0020_Category")); objSPQuery.Query = Query.ToString(); objItems = TasksList.GetItems(objSPQuery); if (objItems != null && objItems.Count > 0) { foreach (SPListItem objItem in objItems) { DisplayColumn = Convert.ToString(objItem["Category_x0020_Name"]); Title = Convert.ToString(objItem["Category_x0020_Name"]); CreateTree(Title, valueArray, null, DisplayColumn, objItem["ID"].ToString()); } } } } } } catch (Exception ex) { throw ex; } } private void CreateTree(string RootNode, string[] valueArray, List<SPListItem> objNodeCollection, string DisplayValue, string KeyValue) { string objExpandValue = string.Empty; TreeNode objTreeNode; TreeNodeCollection objChildNodeColn; try { objTreeNode = new TreeNode(DisplayValue, KeyValue); treeViewCategories.Nodes.Add(objTreeNode); objTreeNode.CollapseAll(); objChildNodeColn = GetChildNode(RootNode, valueArray, objNodeCollection); foreach (TreeNode childnode in objChildNodeColn) { objTreeNode.ChildNodes.Add(childnode); } } catch (Exception ex) { throw ex; } } private TreeNodeCollection GetChildNode(string RootNode, string[] valueArray, List<SPListItem> objListItemColn) { TreeNodeCollection childtreenodes = new TreeNodeCollection(); SPQuery objSPQuery; SPListItemCollection objItems = null; List<SPListItem> objNodeListItems = new List<SPListItem>(); SiteMapNodeCollection objNode = new SiteMapNodeCollection(); objSPQuery = new SPQuery(); string objNodeTitle = string.Empty; string objLookupColumn = string.Empty; StringBuilder Query = new StringBuilder(); SPList objTaskList; SPField spField; string objKeyColumn; try { objTaskList = SPContext.Current.Web.Lists["CategoryDetails"]; objLookupColumn = "Parent_x0020_Category";//objTreeViewControlField.ParentLookup; spField = SPContext.Current.Web.Lists["CategoryDetails"].Fields["Parent Category"]; Query.Append(String.Format(DYNAMIC_CAML_QUERY_GET_CHILD_NODE, spField.InternalName, RootNode)); objSPQuery.Query = Query.ToString(); objItems = objTaskList.GetItems(objSPQuery); foreach (SPListItem objItem in objItems) { objNodeListItems.Add(objItem); } if (objNodeListItems != null && objNodeListItems.Count > 0) { foreach (SPListItem objItem in objNodeListItems) { RootNode = Convert.ToString(objItem["Category_x0020_Name"]); objKeyColumn = Convert.ToString(objItem["ID"]); objNodeTitle = Convert.ToString(objItem["Category_x0020_Name"]); if (!String.IsNullOrEmpty(objNodeTitle)) { TreeNode childNode = new TreeNode(); childNode.Text = objNodeTitle; childNode.Value = objKeyColumn; childNode.CollapseAll(); foreach (TreeNode cnode in GetChildNode(RootNode, valueArray, objListItemColn)) { childNode.ChildNodes.Add(cnode); } childtreenodes.Add(childNode); } } } } catch (Exception ex) { throw ex; } return childtreenodes; // Call method again (recursion) to get the child items } } }