简体   繁体   English

Sharepoint 2010 - webpart treeview不起作用

[英]Sharepoint 2010 - webpart treeview is not working

Hi I am using sharepoint 2010 and am creating a tree view in a webpart to display items from a document library. 您好我正在使用sharepoint 2010并在webpart中创建树视图以显示文档库中的项目。 This code isn't working for me, its displaying everything in the same web... I would like to be able to specify which document library to use. 这段代码对我不起作用,它在同一个网页上显示所有内容......我希望能够指定要使用的文档库。

Also it puts in duplicate nodes in, so if I go to editpage, it adds a duplicate, if I leave edit mode it adds another duplicate. 它也会放入重复的节点,所以如果我去编辑页面,它会添加一个副本,如果我离开编辑模式,它会添加另一个副本。 在此输入图像描述

Can anyone help? 有人可以帮忙吗?

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();

            }
        }
    }
}

尝试在using语句之前添加:

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

Add WebProperties to your WebPart to be able to configure for example the Library which you would like to use instead a hardcoded one. WebProperties添加到WebPart,以便能够配置您要使用的库,而不是硬编码的库。 In this Property you could specify the Lists Name and read it to load this list. 在此属性中,您可以指定列表名称并读取它以加载此列表。

Also to avoid multiple inserts on edit, etc. please add your code inside the Page_Load Event inside 另外,为了避免编辑等多个插入,请在里面的Page_Load事件中添加您的代码

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

This avoids the execution of your code everytime you load or even postback the page and this causes that you add each time a new node to your tree. 这样可以避免每次加载或甚至回发页面时执行代码,这会导致每次向树中添加新节点时都会添加。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM