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

JAD*_*ADE 2 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
Run Code Online (Sandbox Code Playgroud)

如果"ows_ShortenedUrl"存在,我该怎么办?

小智 5

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

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

   //do something with it

}
Run Code Online (Sandbox Code Playgroud)