3

I developed a MS Excel-AddIn with a WPF User Control to display and update MS SharePoint List Data. I used Lists Web Service to communicate with the list data. I can load list data to my Excel worksheet without any problem.

But when I try to update a list item, it doesn't update the item in the SharePoint server. I can't find out what's wrong with my code.

            Excel.Worksheet myWorkSheet = Globals.ThisAddIn.Application.ActiveSheet as Excel.Worksheet;

            Excel.Range cellTitle = myWorkSheet.Cells[2, 1] as Excel.Range;
            Excel.Range cellProduct_SKU = myWorkSheet.Cells[2, 2] as Excel.Range;
            Excel.Range cellProductPrice = myWorkSheet.Cells[2, 3] as Excel.Range;

            string prodName = cellTitle.Value2.ToString();
            string prodSKU = cellProduct_SKU.Value2.ToString();
            string prodPrice = cellProductPrice.Value2.ToString();

            try
            {
                SPListsWS.Lists proxy = new SPListsWS.Lists();
                proxy.Credentials = CredentialCache.DefaultCredentials;
                proxy.Url = "http://mysharepointserver/_vti_bin/Lists.asmx";

                XmlNode listView = proxy.GetListAndView("Products", "");

                string listID = listView.ChildNodes[0].Attributes["Name"].Value;
                string viewID = listView.ChildNodes[1].Attributes["Name"].Value;

                XmlDocument Xdoc = new XmlDocument();
                XmlElement batchElement = Xdoc.CreateElement("Batch");
                batchElement.SetAttribute("OnError", "Continue");
                batchElement.SetAttribute("ListVersion", "1");
                batchElement.SetAttribute("ViewName", viewID);

                batchElement.InnerXml = "<Method ID='1' Cmd='Update'>"
                                      + "<Field Name = 'Title'>" + prodName + "</Field>"
                                      + "<Field Name = 'Product_SKU'>" + prodSKU + "</Field>"
                                      + "<Field Name = 'Product_Price'>" + prodPrice + "</Field>"
                                      + "</Method>";

                XmlNode responseXml = proxy.UpdateListItems("Products", batchElement);
                MessageBox.Show("Item Updated Successfully.");
            }

It shows me the message "Item Updated Successfully" even though it had not been updated.

5

your batchElement.innerXml should include a field for ID as it is necessary for identifying the row in the list which the data should be updated. So changing the code as follows would solve the issue if everything else is ok.

batchElement.InnerXml = "<Method ID='1' Cmd='Update'>"
                                  + "<Field Name = 'ID'>" + prodID + "</Field>" 
                                  + "<Field Name = 'Title'>" + prodName + "</Field>"
                                  + "<Field Name = 'Product_SKU'>" + prodSKU + "</Field>"
                                  + "<Field Name = 'Product_Price'>" + prodPrice + "</Field>"
                                  + "</Method>";

So you should obtain the ID value of the record from the list when you load data to the excel and use that value to assign to the prodID. Hope this would solve your problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.