I recently had to use List.asmx to update metadata properties in sharepoint. I have used it many times before and felt comfortable using it too. But this time there was an error been thrown as "An error occurred while parsing EntityName". After looking into the XML i found that the Title field had special character "&" This was causing the whole problem. The solution was to parse the special character. I wanted to share one of the easiest way to parse the XML.
string s = System.Security.SecurityElement.Escape(str);
This is an easier way to encode the special characters. In a regular .NET application you can parse the entire XML, but donot parse the whole XML that is passed into List.asmx updatelistitems method. This will give you no error but the list will not updated because System.Security.SecurityElement.Escape method will encode all the characters and this will cause issues with your field name, at least it did cause issues for me.
Code Snippet:
XmlDocument xDoc = new XmlDocument();
XmlElement batch = xDoc.CreateElement("Batch");
batch.SetAttribute("OnError", "Continue");
string strBatch = "<Method ID='1' Cmd='New'>" +
"<Field Name='ID'>New</Field>" +
"<Field Name='Title'>" + System.Security.SecurityElement.Escape("A.M. Pappas & Associates") + "</Field>" +
"<Field Name='Test_x0020_Check'>1</Field>" +
"<Field Name='Test_x0020_URL'>http://www.google.com</Field>" +
"<Field Name='Test_x0020_Choice'>InActive</Field>" +
"<Field Name='Test_x0020_User'>" + SPContext.Current.Web.CurrentUser.ID + ";#" +"</Field>" +
"</Method>";
batch.InnerXml = strBatch;
XmlNode node = listws.UpdateListItems("Error List", batch);
There are other ways that you can parse the XML either by stripping out the special characters or replacing with space or using XMLwriter. But I felt this is an easier way and you can never go wrong with this.
string s = System.Security.SecurityElement.Escape(str);
This is an easier way to encode the special characters. In a regular .NET application you can parse the entire XML, but donot parse the whole XML that is passed into List.asmx updatelistitems method. This will give you no error but the list will not updated because System.Security.SecurityElement.Escape method will encode all the characters and this will cause issues with your field name, at least it did cause issues for me.
Code Snippet:
XmlDocument xDoc = new XmlDocument();
XmlElement batch = xDoc.CreateElement("Batch");
batch.SetAttribute("OnError", "Continue");
string strBatch = "<Method ID='1' Cmd='New'>" +
"<Field Name='ID'>New</Field>" +
"<Field Name='Title'>" + System.Security.SecurityElement.Escape("A.M. Pappas & Associates") + "</Field>" +
"<Field Name='Test_x0020_Check'>1</Field>" +
"<Field Name='Test_x0020_URL'>http://www.google.com</Field>" +
"<Field Name='Test_x0020_Choice'>InActive</Field>" +
"<Field Name='Test_x0020_User'>" + SPContext.Current.Web.CurrentUser.ID + ";#" +"</Field>" +
"</Method>";
batch.InnerXml = strBatch;
XmlNode node = listws.UpdateListItems("Error List", batch);
There are other ways that you can parse the XML either by stripping out the special characters or replacing with space or using XMLwriter. But I felt this is an easier way and you can never go wrong with this.
Outstanding. I was having the same exact issue. I tried using ![CDATA[]] to no avail. I also tried setting elBatch.InnerText rather than elBatch.InnerXml, again to no avial.
ReplyDeleteVery nice .. it works.. Thank you.
ReplyDelete