如何在使用SpListItem.Item检索属性之前检查属性是否存在

时间:2012-01-10 05:30:03

标签: api sharepoint

我需要获取SplistItem.Item的值。有一个新属性添加了“ShortenedUrl”。问题是,旧页面不包含此属性,因此每当站点是旧页面时,我都会收到“值不在预期范围内”的错误。

有没有办法首先检查项目是否存在? 在获得价值之前?

这是我的代码。

        '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”存在,我该怎么办?

2 个答案:

答案 0 :(得分:5)

SPFieldCollection.ContainsField只检查架构。您可以在模式中定义字段,该值仍可以为null。如果你知道它在模式中,只需检查项[fieldName] == null。

if (item.Fields.ContainsField(fieldName) && item[fieldName] != null) {

   //do something with it

}

答案 1 :(得分:2)

您可以使用Item.Fields.ContainsField进行检查。