sharepoint 2010 - Can't update item field value?
-
Thursday, August 02, 2012 6:09 PM
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); if (sel_val.Equals("1")) // user clicked up { update_item(item_path); } else // user clicked down { } } 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]; foreach (SPListItem item in List.Items) { if (item.File.ServerRelativeUrl.Equals(item_path)) { item["Order"] = 555; item.Update(); break; } } List.Update(); } protected void refreshbutton_Click(Object sender, EventArgs e) { siteStructure.Nodes.Clear(); create_tree(); } 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(); } } }
All Replies
-
Thursday, August 02, 2012 6:47 PM
You could try a web.Update() after List.Update() in update_item method.
Are you sure that the ListItem is being found in this loop:
foreach (SPListItem item in List.Items) { if (item.File.ServerRelativeUrl.Equals(item_path)) { item["Order"] = 555; item.Update(); break; } }Can you add some code to debug/verify that the if block is being entered and the Item["Order"] = 555 is in fact being executed?
Stephen P. Kappel - Sharepoint Developer
-
Thursday, August 02, 2012 6:54 PMCan you suggest me a good way to debug that, how can I add a popup box? A popup box would work well in this case.
EDIT: just tried the web.Update() and that didnt work, also tried moving the code from update_item to the page load and that didn't make it work either.... :(- Edited by ryands Thursday, August 02, 2012 7:04 PM
-
Thursday, August 02, 2012 7:11 PM
The easiest way to get a popup box-like message could be to add a Label to the web part and set its Text property.
Add a Textbox txtTest to the bottom of the web part and set the Text property to "". Then, in the if block add txtTest.Text+="If block entered. ";
If you see the message appear on your web part, the item["Order"] = 555; code is being called. Otherwise, it is not, and you know where the problem lies.
Stephen P. Kappel - Sharepoint Developer
-
Thursday, August 02, 2012 7:24 PM
Looking further at your code, I'd recommend using the ListItem's ID as the Value property on your TreeNodes rather than the relative URL. It will make your code much cleaner. You could then replace your foreach loop with List.GetItemById(item_id). In my opinion IDs are the best way of identifying, remembering, and fetching SharePoint ListItems. I think if you update your code using IDs your life will become a lot easier...
Good luck!
Stephen P. Kappel - Sharepoint Developer
-
Thursday, August 02, 2012 7:27 PM
Can you show me how to modify the code? I'm kinda lost here.
-
Thursday, August 02, 2012 7:44 PM
This is not tested, but I was thinking something along these lines:
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); int item_id = Int32.Parse(siteStructure.SelectedValue.Substring(2)); if (sel_val.Equals("1")) // user clicked up { update_item(item_id); } else // user clicked down { } } protected void update_item(int item_id) { 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; web.Update(); SPList List = web.Lists[DocLib]; SPListItem item = List.GetItemById(item_id); item["Order"] = 555; item.Update(); List.Update(); } protected void refreshbutton_Click(Object sender, EventArgs e) { siteStructure.Nodes.Clear(); create_tree(); } 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, item.ID, null, item.ServerRelativeUrl, "_blank"); sub_parent.ChildNodes.Add(node); current_leaf = node; node = new TreeNode("Up", "1_" + item.ID, null, null, null); current_leaf.ChildNodes.Add(node); node = new TreeNode("Down", "0_" + item.ID, null, null, null); current_leaf.ChildNodes.Add(node); } } siteStructure.ExpandAll(); } } }
I hope that helps.
Stephen P. Kappel - Sharepoint Developer
-
Thursday, August 02, 2012 7:51 PM
I tried the method you suggested about writing to a label, and i see there was a problem with the handler not registering, then I put the line that created the handler outside and below the if statement of the page load method, and i saw a change in the label text. Yet it still didn't update the order field.
Here is my new updated code:
The label text had "got 5" when i tested, but I still dont see the item order value change..... This is most confusing problem yet lol...
EDIT: also from your code SPFile doesn't have a method ID...
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)) { Label1.Text = "got 5"; item["Order"] = 555; item.Update(); 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(); } } }
- Edited by ryands Thursday, August 02, 2012 8:05 PM
-
Thursday, August 02, 2012 8:06 PM
Try calling web.Update after setting AllowUnsafeUpdates to true and before updating the item.
Is it possible that the wrong ListItem is being updated? Are any of the Order fields set to 555?
Is Order a field you created yourself? Have you ever changed the name of the field? I just want to make sure the internal name of the field is the same as the external one you are using...
Stephen P. Kappel - Sharepoint Developer
-
Thursday, August 02, 2012 8:27 PM
I tried changing the name of the field from Order to Order Item By and that made it work, i think there is an internal field called Order used by the links content type. -
Thursday, August 02, 2012 8:44 PMI have another question, for a file object of type
SPFile, how can I get a field value from it, I can do the ["ID"] because it doesn't support that method...
-
Friday, August 03, 2012 1:25 PM
I don't know if I understand your question correctly, but you can do SPFile.Item.ID.
See http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spfile.item
Stephen P. Kappel - Sharepoint Developer

