SharePoint 权限提升的方法

普通方法

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

构造一下

Macroresolute.SharePoint.Utility.Privileges

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
    }
}


 授权

#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);


 

你可能感兴趣的:(SharePoint 权限提升的方法)