如何使用SpListItem.Item检索它之前是否存在属性
问题描述:
我需要获取SplistItem.Item的值。有一个新的属性添加了“ShortenedUrl”。问题在于,旧网页不包含此属性,因此每当网站是旧网页时,我都会收到“值不在预期范围内”的错误。如何使用SpListItem.Item检索它之前是否存在属性
有没有办法首先检查项目是否存在?获得该值之前是否有 ?
这是我的代码。
'Set shortened URL
Dim objShortUrl As Object = postItemById.Item("ows_ShortenedUrl")
If objShortUrl IsNot Nothing Then
blogPost.shortURL = objShortUrl
Else
blogPost.shortURL = DBNull.Value
End If
如果“ows_ShortenedUrl”存在,我该怎么办?
答
您可以使用Item.Fields.ContainsField来检查。
答
SPFieldCollection.ContainsField只是检查架构。您可以在模式中定义一个字段,并且该值仍可以为空。如果你知道它在模式中,只需检查项[fieldName] == null。
if (item.Fields.ContainsField(fieldName) && item[fieldName] != null) {
//do something with it
}