2012-07-27 103 views
0

您好我正在使用SharePoint 2010,並在webpart中創建樹視圖以顯示文檔庫中的項目。此代碼不適用於我,它顯示在同一網絡中的所有內容... 我希望能夠指定使用哪個文檔庫。Sharepoint 2010 - webpart treeview無法正常工作

另外它放入重複節點,所以如果我去編輯頁面,它會添加一個重複的,如果我離開編輯模式,它會添加另一個重複。 enter image description here

任何人都可以幫忙嗎?

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

      } 
     } 
    } 
} 

回答

2

嘗試的使用語句之前添加此:

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

添加WebProperties到你的WebPart到能夠配置例如,你想改用硬編碼的一個圖書館。在此屬性中,您可以指定列表名稱並讀取它以加載此列表。

同時,爲了避免在編輯多個插件等請添加代碼的Page_Load事件裏面裏面

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

這避免你的代碼的執行每次加載甚至投遞的頁面,這將導致你每次將新節點添加到您的樹中。