具有提升权限的 Sharepoint 查询

发布于 2024-11-07 19:40:52 字数 1009 浏览 1 评论 0原文

Web 部件需要访问 Sharepoint 列表(读取模式)。如果用户是管理员,则没有问题(按预期工作),但如果用户没有访问权限,我必须使用“RunWithElevatedPrivileges”方法。

问题是查询似乎没有返回正确的结果。我缺少什么?

        SPList demoList = null;

        SPSecurity.RunWithElevatedPrivileges(delegate()
        {
            SPSite oSite = SPControl.GetContextSite(HttpContext.Current); // ADDED
            SPWeb oWeb = oSite.OpenWeb();                                 // ADDED
            demoList = oWeb.Lists["nameList"];
        });
        // demoList has 3 Elements (admin and no admin user) OK

        SPListItemCollection collListItems = null;

        SPSecurity.RunWithElevatedPrivileges(delegate()
        {
            SPQuery oQuery = new SPQuery() { Query = "<OrderBy><FieldRef Name='Date' Ascending='False' /></OrderBy>" };
            collListItems = demoList.GetItems(oQuery);
        });

        // 
        //IF ADMIN
        //collListItems.Count ==>3

        //IF NO ADMIN 
        //collListItems.Count ==>0

A Webpart needs to access a Sharepoint List (read mode). If the user is admin, there isn't problem (works as espected), but if the user hasn't permissions to access, I must use "RunWithElevatedPrivileges" method.

The problem is that seems that the query don't return the correct results. What I'm missing?

        SPList demoList = null;

        SPSecurity.RunWithElevatedPrivileges(delegate()
        {
            SPSite oSite = SPControl.GetContextSite(HttpContext.Current); // ADDED
            SPWeb oWeb = oSite.OpenWeb();                                 // ADDED
            demoList = oWeb.Lists["nameList"];
        });
        // demoList has 3 Elements (admin and no admin user) OK

        SPListItemCollection collListItems = null;

        SPSecurity.RunWithElevatedPrivileges(delegate()
        {
            SPQuery oQuery = new SPQuery() { Query = "<OrderBy><FieldRef Name='Date' Ascending='False' /></OrderBy>" };
            collListItems = demoList.GetItems(oQuery);
        });

        // 
        //IF ADMIN
        //collListItems.Count ==>3

        //IF NO ADMIN 
        //collListItems.Count ==>0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

弥繁 2024-11-14 19:40:53

您需要创建具有提升权限的新对象。

SPSecurity.RunWithElevatedPrivileges(delegate()
{
    SPSite oSite = new SPSite(SPContext.Current.Site.ID); 
    SPWeb oWeb = oSite.OpenWeb(SPContext.Current.Web.ID);                                 
    demoList = oWeb.Lists["nameList"];
});

此外,您应该处理新创建的对象,并且不需要两个委托。

SPSecurity.RunWithElevatedPrivileges(delegate {
    using (SPSite oSite =new SPSite(SPContext.Current.Site.ID))
    using (SPWeb oWeb = oSite.OpenWeb()) {
        var demoList = oWeb.Lists["nameList"];
        SPQuery oQuery = new SPQuery
                            { Query = "<OrderBy><FieldRef Name='Date' Ascending='False' /></OrderBy>" };
        SPListItemCollection collListItems = demoList.GetItems(oQuery);

        //IF ADMIN
        //collListItems.Count ==>3

        //IF NO ADMIN 
        //collListItems.Count ==>0
    }
});

You need to create new object with elevated privieges.

SPSecurity.RunWithElevatedPrivileges(delegate()
{
    SPSite oSite = new SPSite(SPContext.Current.Site.ID); 
    SPWeb oWeb = oSite.OpenWeb(SPContext.Current.Web.ID);                                 
    demoList = oWeb.Lists["nameList"];
});

Also, you should dispose of the newly created objects and there is no need for two delegates.

SPSecurity.RunWithElevatedPrivileges(delegate {
    using (SPSite oSite =new SPSite(SPContext.Current.Site.ID))
    using (SPWeb oWeb = oSite.OpenWeb()) {
        var demoList = oWeb.Lists["nameList"];
        SPQuery oQuery = new SPQuery
                            { Query = "<OrderBy><FieldRef Name='Date' Ascending='False' /></OrderBy>" };
        SPListItemCollection collListItems = demoList.GetItems(oQuery);

        //IF ADMIN
        //collListItems.Count ==>3

        //IF NO ADMIN 
        //collListItems.Count ==>0
    }
});
一片旧的回忆 2024-11-14 19:40:53

如果您在提升的块之外创建站点和 Web 对象(或从当前 SPContext 访问它们),它们将拥有当前登录用户的权限。因此,您的查询即使位于提升的块中,也没有使用提升的权限。您需要在提升的块内创建新的站点和 Web 对象,然后访问列表,然后运行查询以获得预期结果。

这是进一步解释的资源。尽管它是针对 SharePoint 2007 完成的,但它也适用于 SharePoint 2010。

在 Windows SharePoint Services 3.0 中以提升的权限运行命令
http://msdn.microsoft.com/en-我们/library/bb466220(v=office.12).aspx

If you create the Site and Web objects (or access them from the current SPContext) outside the elevated block, they will have the permissions of the currently logged-in user. So your query, even though it is in an elevated block, is not using elevated permissions. You need to create new Site and Web objects inside the elevated block, then get access to the list, and then run the query to get the expected results.

Here's a resource that explains further. Even though it was done for SharePoint 2007, it applies to SharePoint 2010.

Running Commands with Elevated Privileges in Windows SharePoint Services 3.0
http://msdn.microsoft.com/en-us/library/bb466220(v=office.12).aspx

与他有关 2024-11-14 19:40:53

如果要在 SharePoint 列表中包含写入操作,请在 RWEP 方法之前添加 SPWeb.ValidateFormDigest()SPUtility.ValidateFormDigest() 行。

SPUtility.ValidateFormDigest();
SPSecurity.RunWithElevatedPrivileges(delegate()
{

}

If you want to include a write operation in SharePoint List, then add SPWeb.ValidateFormDigest() or SPUtility.ValidateFormDigest() line before RWEP Method.

SPUtility.ValidateFormDigest();
SPSecurity.RunWithElevatedPrivileges(delegate()
{

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文