【问题标题】:Sharepoint 2010 - webpart treeview is not workingSharepoint 2010 - webpart 树视图不工作
【发布时间】:2012-07-26 09:16:34
【问题描述】:

您好,我正在使用 sharepoint 2010,并正在 Web 部件中创建树视图以显示文档库中的项目。这段代码对我不起作用,它在同一个网络中显示所有内容...... 我希望能够指定要使用的文档库。

它还放入了重复的节点,所以如果我进入编辑页面,它会添加一个重复,如果我离开编辑模式,它会添加另一个重复。

谁能帮忙?

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using System.Web;
using System.IO;

namespace VisualWebPartProject1.VisualWebPart1 
{
    public partial class VisualWebPart1UserControl : UserControl 
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            SPWeb thisWeb = null;
            TreeNode node;
            using (thisWeb = SPContext.Current.Web)
            {




                //Add the Web's title as the display text for the tree node, and add the URL as the NavigateUri.
                node = new TreeNode(thisWeb.Title, null, null, thisWeb.Url, "_self");
                //The Visual Web Part has a treeview control called siteStructure.
                siteStructure.Nodes.Add(node);
                //Get a reference to the current node, so child nodes can be added in the correct position.
                TreeNode parentNode = node;
                //Iterate through the Lists collection of the Web.


                /*
                foreach (SPListItem item in myList.Items)
                {
                    SPFieldUrlValue data = item["Url"] as SPFieldUrlValue;
                    // now you have data.Description, data.Url
                    node = new TreeNode(Path.GetFileName(data.Url), null, null, data.Url, "_self");
                    parentNode.ChildNodes.Add(node);

                }
                */



                foreach (SPList list in thisWeb.Lists)
                {
                    if (!list.Hidden)
                    {
                        node = new TreeNode(list.Title, null, null, list.DefaultViewUrl, "_self");
                        parentNode.ChildNodes.Add(node);
                    }
                }
                foreach (SPWeb childWeb in thisWeb.Webs)
                {
                    //Call our own helper function for adding each child Web to the tree.
                    addWebs(childWeb, parentNode);
                    childWeb.Dispose();
                }


                siteStructure.CollapseAll();

            }
        }
        void addWebs(SPWeb web, TreeNode parentNode)
        {
            TreeNode node;
            node = new TreeNode(web.Title, null, null, web.Url, "_self");
            parentNode.ChildNodes.Add(node);
            parentNode = node;
            foreach (SPList list in web.Lists)
            {
                if (!list.Hidden)
                {
                    node = new TreeNode(list.Title, null, null, list.DefaultViewUrl, "_self");
                    parentNode.ChildNodes.Add(node);
                }
            }
            foreach (SPWeb childWeb in web.Webs)
            {
                //Call the addWebs() function from itself (i.e. recursively) 
                //to add all child Webs until there are no more to add.
                addWebs(childWeb, parentNode);
                childWeb.Dispose();

            }
        }
    }
}

【问题讨论】:

    标签: c# visual-studio-2010 sharepoint-2010


    【解决方案1】:

    尝试在你的 using 语句之前添加这个:

    If(node.Nodes.Count == 0) { // The rest of your code here }
    

    【讨论】:

      【解决方案2】:

      WebProperties 添加到您的WebPart 以便能够配置例如您想使用的库而不是硬编码的库。在此属性中,您可以指定列表名称并读取它以加载此列表。

      另外为了避免编辑时多次插入等,请在Page_Load事件里面添加你的代码

      if (!Page.IsPostBack)
      {
          Your code goes here...
      }
      

      这避免了每次加载甚至回发页面时都执行代码,这会导致您每次向树中添加一个新节点。

      【讨论】:

        猜你喜欢
        • 2011-11-03
        • 1970-01-01
        • 2011-11-01
        • 2011-03-26
        • 2011-03-29
        • 1970-01-01
        • 2011-07-31
        • 2011-08-24
        • 2011-10-08
        相关资源
        最近更新 更多