Table of Contents

RunWithElevatedPrivileges

Metoda System.RunWithElevatedPrivileges umožňuje spouštet kód s plnými právy (Full Control). Hodí se pokud potřebujeme nějakému obyčejnému uživateli umožnit nastavovat vlastnosti, které mu jinak najsou přístupné (např. přes Webpart).

V metodě se musí vytvořit SPSite, nemůžeme použít SPContext, práva by zůstala stejná.

Examples

Easy use

SPSecurity.RunWithElevatedPrivileges(delegate()
{
    using (SPSite site = new SPSite(siteURL))
    {
    // code
    }
});

Delegate method

        private SPSite oSPSite = null;
        private SPWeb oSPWeb = null;
 
        /// <summary>
        /// This Method is called throug Delegate
        /// </summary>
        private void OpenSPWeb()
        {
            oSPSite = new SPSite("http://srv-moss-tp1:36000");
            oSPWeb = oSPSite.OpenWeb();
        }
 
        /// <summary>
        /// If you click the Button to establish a connection to your SharePoint-Site,
        /// you run this code with elevated Privileges.
        /// </summary>
        private void btn_Click(object sender, EventArgs e)
        {
            SPSecurity.CodeToRunElevated elevatedOpenSPWeb = new SPSecurity.CodeToRunElevated(OpenSPWeb);
            SPSecurity.RunWithElevatedPrivileges(elevatedOpenSPWeb);
 
            // code
 
        }

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spsecurity.runwithelevatedprivileges.aspx
http://blog.magenic.com/blogs/derekp/archive/2008/01/10/SPSecurity.RunWithElevatedPrivileges.aspx
http://littletalk.wordpress.com/2008/12/10/unable-to-evaluate-expression-because-the-code-is-optimized-or-a-native-frame-is-on-top-of-the-call-stack-when-use-new-spsite/
http://moldenco.blogspot.com/2007/05/spsecurityrunwithelevatedprivileges-to.html