Updating field with text containing &

Jun 27, 2011 at 11:12 AM
Edited Jun 27, 2011 at 11:15 AM

While using the GetListItems with the UpdateListItems function to copy data from one list to another, some updates would fail without any apparent reason. After many hours of hair pulling, i finally discovered that fields containing & characters would not copy. Fortunately, i can change my data to prevent this but I'm worried about how fragile this will be. Could someone please explain to me why this happens and if there is anything i can do to provide a more robust updating process.

Also, are there any other special characters that would cause this same behavior? symbols such as > <  ( ),- and $ don't seem to cause any problems

TIA
Dean

Jun 27, 2011 at 12:24 PM

Yes, an unfortunate side effect of dealing with data in XML format.  &, <, >, and " (double quote) are reserved characters and need to be escaped properly.  I've read single quotes (apostrophe) needs to be escaped as well, but I haven't ran into issues with that character.  Fortunately, there is a function that ships with SharePoint to do that.  Just run this function on your values before sending them into the UpdateListItems portion of your script:

STSHtmlEncode(yourValueHere);

 

Cheers,

Matt

Coordinator
Jun 27, 2011 at 12:24 PM

Dean:

Ampersands are special characters and need to be escaped in many cases. If you replace the & with &amp; you should be able to do the update.

Internally to SPServices, I use this function:

 // Escape column values
 function escapeColumnValue(s) {
  return s.replace(/&(?![a-zA-Z]{1,8};)/g, "&amp;");
 }

M.

Jun 27, 2011 at 1:38 PM
iOnline247 wrote:

Yes, an unfortunate side effect of dealing with data in XML format.  &, <, >, and " (double quote) are reserved characters and need to be escaped properly.  I've read single quotes (apostrophe) needs to be escaped as well, but I haven't ran into issues with that character.  Fortunately, there is a function that ships with SharePoint to do that.  Just run this function on your values before sending them into the UpdateListItems portion of your script:

 

STSHtmlEncode(yourValueHere);

 

 

Cheers,

Matt


THANK YOU, THANK YOU, THANK YOU. I hope that I can provide some useful advice to you someday.

Dean

Jun 27, 2011 at 4:03 PM

Thanks! I look forward to learning from you in the future.  If you want to accelerate your learning, may I suggest www.uspja.com.  It's a great place for SharePoint training.