0
votes

Hi I am using sharepoint 2010 and am creating a tree view in a webpart to display items from a document library. 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.enter image description here

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

            }
        }
    }
}
2

2 Answers

2
votes

Try adding this before your using statement:

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

Add WebProperties to your WebPart to be able to configure for example the Library which you would like to use instead a hardcoded one. 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

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.