{sharepoint}提升 SharePoint 代码执行权限

提升 SharePoint 代码执行权限

关于如何提升 SharePoint 代码执行权限及相关知识介绍的文章我们园子里有很多,

这里给出其中两篇文章的链接,就不再啰嗦了。

http://www.cnblogs.com/llbofchina/archive/2008/05/24/1206393.html

http://www.cnblogs.com/nirvanalst/archive/2008/10/13/1310411.html

 

由于项目中经常需要提升某些代码的执行权限,每次都需要写类似于下面的代码:

复制代码
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite Site = new SPSite(SiteId))
{
using (SPWeb Web = Site.OpenWeb(WebUrl))
{
...
}
}
});
复制代码

久而久之,项目中充斥着“相同的”代码,而且看起来也不是很优雅,

就连使用 “EventHandler Explorer”为表单库、列表绑定事件处理程序时,也多出了

诸如于 <>c__DisplayClass1、<>c__DisplayClass2 等自动生成的类。

考虑到其复用性,故对其作如下封装: 

复制代码
using System;
using Microsoft.SharePoint;

namespace Pturesoft.Utility.Handler
{
/// <summary>
/// 提供了在 SharePoint 站点和网站上提升方法执行权限所需的功能。无法继承此类。
/// </summary>
public static class Privileges
{
#region static Privileges --- 构造函数
static Privileges()
{
//暂无任何实现。
}
#endregion

#region public delegate [void] PrivilegeMethod --- 表示需要提升执行权限的方法
/// <summary>
/// 表示需要提升执行权限的方法。
/// </summary>
/// <param name="oSite">表示一个包括顶级网站和所有子网站的 SharePoint 站点的集合。</param>
/// <param name="oWeb">表示一个 SharePoint 网站。</param>
/// <param name="args">该方法执行时所需的参数。不需要该参数时,请指定为 null。</param>
public delegate void PrivilegeMethod(SPSite oSite, SPWeb oWeb, Object args);
#endregion

#region public static [void] Elevated --- 在当前站点和当前网站上提升方法的执行权限
/// <summary>
/// 在当前站点和当前网站上提升方法的执行权限。
/// </summary>
/// <param name="privilegeMethod">需要提升执行权限的方法。</param>
/// <param name="args">该方法执行时所需的参数。不需要该参数时,请指定为 null。当需要为 SharePoint 事件处理程序提升方法的执行权限时,该参数必须为 SharePoint 事件的 Microsoft.SharePoint.SPItemEventProperties 消息对象。</param>
public static void Elevated(PrivilegeMethod privilegeMethod, Object args)
{
Guid SiteID;
String WebUrl = null;

if (args is SPItemEventProperties) //事件处理程序
{
SPItemEventProperties Properties = args as SPItemEventProperties;

SiteID = Properties.SiteId;
WebUrl = Properties.RelativeWebUrl;
}
else //页面请求
{
SiteID = SPContext.Current.Site.ID;
WebUrl = SPContext.Current.Web.ServerRelativeUrl;
}

SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite oSite = new SPSite(SiteID))
{
using (SPWeb oWeb = oSite.OpenWeb(WebUrl))
{
privilegeMethod(oSite, oWeb, args);
}
}
});
}
#endregion

#region public static [void] Elevated --- 为 SharePoint 事件处理程序提升执行权限
/// <summary>
/// 为 SharePoint 事件处理程序提升执行权限。
/// </summary>
/// <param name="privilegeMethod">需要提升执行权限的方法。</param>
/// <param name="properties">SharePoint 事件的 Microsoft.SharePoint.SPItemEventProperties 消息对象。</param>
/// <param name="args">该方法执行时所需的参数。不需要该参数时,请指定为 null。</param>
public static void Elevated(PrivilegeMethod privilegeMethod, SPItemEventProperties properties, Object args)
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite oSite = new SPSite(properties.SiteId)) { using (SPWeb oWeb = oSite.OpenWeb(properties.RelativeWebUrl)) { privilegeMethod(oSite, oWeb, args); } } }); } #endregion #region public static [void] Elevated --- 在当前站点和指定的网站上提升方法的执行权限 /// <summary> /// 在当前站点和指定的网站上提升方法的执行权限。 /// </summary> /// <param name="privilegeMethod">需要提升执行权限的方法。</param> /// <param name="webUrl">一个字符串,包含相对于服务器或相对于网站的的 URL。相对于服务器的 URL 以正斜杠 ("/"),开始,而相对于网站的 URL 不以正斜杠开头。</param> /// <param name="args">该方法执行时所需的参数。不需要该参数时,请指定为 null。</param> public static void Elevated(PrivilegeMethod privilegeMethod, String webUrl, Object args) { Guid SiteID = SPContext.Current.Site.ID; SPSecurity.RunWithElevatedPrivileges(delegate() { using (SPSite oSite = new SPSite(SiteID)) { using (SPWeb oWeb = oSite.OpenWeb(webUrl)) { privilegeMethod(oSite, oWeb, args); } } }); } #endregion #region public static [void] Elevated --- 在指定的站点和指定网站上提升方法的执行权限 /// <summary> /// 在指定的站点和指定网站上提升方法的执行权限。 /// </summary> /// <param name="privilegeMethod">需要提升权限的方法。</param> /// <param name="siteUrl">一个字符串,该字符串指定网站集的绝对 URL。</param> /// <param name="webUrl">一个字符串,包含相对于服务器或相对于网站的的 URL。相对于服务器的 URL 以正斜杠 ("/"),开始,而相对于网站的 URL 不以正斜杠开头。</param> /// <param name="args">该方法执行时所需的参数。不需要该参数时,请指定为 null。</param> public static void Elevated(PrivilegeMethod privilegeMethod, String siteUrl, String webUrl, Object args) { SPSecurity.RunWithElevatedPrivileges(delegate() { using (SPSite oSite = new SPSite(siteUrl)) { using (SPWeb oWeb = oSite.OpenWeb(webUrl)) { privilegeMethod(oSite, oWeb, args); } } }); } #endregion }}
复制代码

注:均提升到系统权限级别!

 

封装后,将需要提升执行权限的方法、方法所需参数传递给静态方法 Privileges.Elevated 即可。

需要提升执行权限的方法签名如下:  

复制代码
#region private [void] Testing --- 表示需要提升执行权限的方法
/// <summary>
/// 表示需要提升执行权限的方法。
/// </summary>
/// <param name="oSite">表示一个包括顶级网站和所有子网站的 SharePoint 站点的集合。</param>
/// <param name="oWeb">表示一个 SharePoint 网站。</param>
/// <param name="args">该方法执行时所需的参数。不需要该参数时,请指定为 null。</param>
private void Testing(SPSite oSite, SPWeb oWeb, object args)
{
...
}
#endregion
复制代码

提升方法的执行权限时,有以下四种可选重载:

复制代码
//重载一:适用于 InfoPath 表单(浏览器模式)、WebPart、页面请求、SharePoint 事件处理程序(参数必须为事件的 SPItemEventProperties 消息对象)
Privileges.Elevated(this.Testing, null);

//重载二:仅适用于 SharePoint 事件处理程序
Privileges.Elevated(this.Testing, properties, null);

//重载三:适用于 InfoPath 表单(浏览器模式)、WebPart、页面请求
Privileges.Elevated(this.Testing, "webUrl", null);

//重载四:适用于任何情况
Privileges.Elevated(this.Testing, "siteUrl", "webUrl", null);
复制代码

源文件:http://files.cnblogs.com/zys529/Privileges.rar

MD5:B288FF01FDFBF9E0F82F5B1DEF6E3800

原文地址:https://www.cnblogs.com/Areas/p/5501323.html