SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have this code here from a webpart that creates a treeview from a document library.

For each item in the list it will create two child nodes called "Up" and "Down". When you click up, it should change the parent node (which is the item in the document library) item's field called 'Order' the value 555. But when I try this and then go to the item, the order value didn't change. Does anyone see the problem?


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 String DocLib = "MyTestDocLib";

        protected void Page_Load(object sender, EventArgs e)
            if (siteStructure.FindNode(DocLib) == null)
            siteStructure.SelectedNodeChanged += new EventHandler(tree_Select);

        void tree_Select(Object sender, EventArgs e)
            string sel_val = siteStructure.SelectedValue.Substring(0, 1);
            string item_path = siteStructure.SelectedValue.Substring(2, siteStructure.SelectedValue.Length - 2);

            Label1.Text = "got 1";
            if (sel_val.Equals("1")) // user clicked up
                Label1.Text = "got 2";
            else // user clicked down
                Label1.Text = "got 1.5";

        protected void update_item(String item_path)
            SPSite site = SPContext.Current.Site;
            SPWeb web = SPContext.Current.Web;
            //Need to set AllowUnsafeUpdates to true so you use the code to update the list.
            web.AllowUnsafeUpdates = true;
            SPList List = web.Lists[DocLib];
            SPListItemCollection listItems = List.GetItems();
            Label1.Text = "got 3";
            foreach (SPListItem item in listItems)
                Label1.Text = "got 4";

                if (item.File.ServerRelativeUrl.Equals(item_path))
                    item["Order"] = 555;
                    Label1.Text = "got 5";

        protected void refreshbutton_Click(Object sender, EventArgs e)
            Label1.Text = "Label";

        protected void create_tree()
            SPWeb thisWeb = null;
            thisWeb = SPContext.Current.Web;

            SPList myList = thisWeb.Lists[DocLib];
            SPFolder my_folder = myList.RootFolder;

            TreeNode node;
            node = new TreeNode(myList.Title, null, null, myList.DefaultViewUrl, "_blank");
            TreeNode parentNode = node;

            TreeNode sub_parent, current_leaf;

            foreach (SPFolder folder in my_folder.SubFolders)
                node = new TreeNode(folder.Name, null, null, folder.ServerRelativeUrl, "_blank");
                sub_parent = node;

                foreach (SPFile item in folder.Files)
                    node = new TreeNode(item.Name, null, null, item.ServerRelativeUrl, "_blank");
                    current_leaf = node;

                    node = new TreeNode("Up", "1_" + item.ServerRelativeUrl, null, null, null);
                    node = new TreeNode("Down", "0_" + item.ServerRelativeUrl, null, null, null);

share|improve this question
Have you tried to attach a debugger? I'm pretty sure your tree_Select is never called as you only attach it to the event when siteStructure is empty. – Per Jakobsen Aug 2 '12 at 19:46
@Per Jakobsen, I updated my code in the above post, I used a label to do some debugging, and I noticed that it said "got 5" after clicking on "Up". So I know it went into the part to update the order value. But after when I went to check the value, it was still the same... – omega Aug 2 '12 at 20:12
Have you tried debugging it? – Mike Aug 7 '12 at 17:17

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.