Joe 的个人资料Joe Shepherd - Adventure...照片日志列表更多 ![]() | 帮助 |
|
4月6日 Handling special characters when working with SPListItems in codeSooner or later you are going to have a need to write a routing that will compare the items in a SharePoint list to records in a SQL Database. Recently I had such a need and I ended up with a few lessons learned so I thought I would share them with you all.
First thing you need to understand is that SPListItems are HTML Encoded so when you compare them to a regular string you first need to Decode them to get an accurate comparison. Do this by using the HttpUtility.HtmlDecode() method. If you try to compare the following entry in a database to the same entry in a SPList the comparison will fail if there are special characters in the field such as '&'.
if (_listItem["Description"].ToString() != row["ItemDescription"].ToString())
{
_listItem["Description"] = row["ItemDescription"].ToString();
_listItem.Update();
updated = true;
}
The above code will always return false because the '&' symbol in the SPList is encoded as '&'. You must first Decode the values from the list to make an accurate comparison.
if (HttpUtility.HtmlDecode(_listItem["Description"].ToString()) != row["ItemDescription"].ToString())
{
_listItem["Description"] = row["ItemDescription"].ToString();
_listItem.Update();
updated = true;
}
引用通告此日志的引用通告 URL 是: http://joeshepherd.spaces.live.com/blog/cns!9AE2097A4A610B63!130.trak 引用此项的网络日志
|
|
|