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?
Thanks
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)
{
create_tree();
}
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";
update_item(item_path);
}
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;
item.Update();
Label1.Text = "got 5";
break;
}
}
List.Update();
web.Update();
}
protected void refreshbutton_Click(Object sender, EventArgs e)
{
siteStructure.Nodes.Clear();
create_tree();
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");
siteStructure.Nodes.Add(node);
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");
parentNode.ChildNodes.Add(node);
sub_parent = node;
foreach (SPFile item in folder.Files)
{
node = new TreeNode(item.Name, null, null, item.ServerRelativeUrl, "_blank");
sub_parent.ChildNodes.Add(node);
current_leaf = node;
node = new TreeNode("Up", "1_" + item.ServerRelativeUrl, null, null, null);
current_leaf.ChildNodes.Add(node);
node = new TreeNode("Down", "0_" + item.ServerRelativeUrl, null, null, null);
current_leaf.ChildNodes.Add(node);
}
}
siteStructure.ExpandAll();
}
}
}