具有提升权限的 Sharepoint 查询
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要创建具有提升权限的新对象。
此外,您应该处理新创建的对象,并且不需要两个委托。
You need to create new object with elevated privieges.
Also, you should dispose of the newly created objects and there is no need for two delegates.
如果您在提升的块之外创建站点和 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
如果要在 SharePoint 列表中包含写入操作,请在 RWEP 方法之前添加
SPWeb.ValidateFormDigest()
或SPUtility.ValidateFormDigest()
行。If you want to include a write operation in SharePoint List, then add
SPWeb.ValidateFormDigest()
orSPUtility.ValidateFormDigest()
line before RWEP Method.