0

我正在使用 CAML Queryy 来获取所有包含 ContentType 的列表项,但我还需要知道当前用户是否有权查看该文件。

那部分我不知道我该如何检查它。

我将此示例用作如何获取与内容类型相关的项目的参考。

https://sharepoint.stackexchange.com/questions/14566/how-to-find-all-documents-of-a-certain-content-type

谢谢。

4

1 回答 1

1

默认情况下,在 SharePoint 中,我们的代码运行模拟为执行 Web 请求的用户。因此,由 CAML 查询返回的项目已经进行了安全修整。意思是,结果集只包含当前用户被允许“看到”的项目。

在某些情况下,您需要使用系统权限执行 CAML 查询。为此SPSite,必须使用系统帐户令牌打开对象:

using (SPSite elevatedSite = new SPSite("http://server-url", SPUserToken.SystemAccount))
{
  // open web; list; 
  // execute caml query with system account priveliges.
}

在这种情况下,您可以使用以下方法检查/确保对某个列表项的权限DoesUserHavePermissions

SPListItem item = //...
if (item.DoesUserHavePermissions(SPContext.Current.Web.CurrentUser, SPBasePermissions.ViewListItems))
{
  // futher actions if user has permission goes here.
}

需要注意的重要一点是,您必须DoesUserHavePermissions使用SPUser参数调用 the 的重载。没有的重载将使用站点的“当前用户”。自使用系统帐户令牌打开站点以来,系统帐户是哪个。

于 2011-06-28T08:52:19.460 回答