Joe 的个人资料Joe Shepherd - Adventure...照片日志列表更多 工具 帮助

日志


4月6日

Handling special characters when working with SPListItems in code

Sooner 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;
  }
 
 

评论

请稍候...
很抱歉,您输入的评论太长。请缩短您的评论。
您没有输入任何内容,请重试。
很抱歉,我们当前无法添加您的评论。请稍后重试。
若要添加评论,需要您的家长授予您相应权限。请求权限
您的家长禁用了评论功能。
很抱歉,我们当前无法删除您的评论。请稍后重试。
您已超过了一天之内允许提供的评论数上限。请在 24 小时后重试。
因为我们的系统表明您可能在向其他用户提供垃圾评论,您的帐户已禁用了评论功能。如果您认为我们错误地禁用了您的帐户,请联系 Windows Live 支持部门
完成下面的安全检查,您提供评论的过程才能完成。
您在安全检查中键入的字符必须与图片或音频中的字符一致。
ShepherdJo​e 在此页禁用了评论功能。

引用通告

此日志的引用通告 URL 是:
http://joeshepherd.spaces.live.com/blog/cns!9AE2097A4A610B63!130.trak
引用此项的网络日志