Archive

Posts Tagged ‘Elevated Privileges’

Sharepoint 2010 run with elevated privileges

April 22nd, 2011 No comments

When I was programming for MOSS 2007 I often used the “Run with Elevated Privileges” delegate.

            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite site = new SPSite(SPContext.Current.Site.Url))
                {
                   using (SPWeb web = site.OpenWeb())
                    {
                    }
                }

            });

But I am now developing for Sharepoint 2010 and sometimes (for no clear reason) this code doesn’t work. I found another way to do this. This is by passing a sys token during creation of SPSite.

            SPUserToken sysToken = SPContext.Current.Site.SystemAccount.UserToken;

            using (var site = new SPSite(SPContext.Current.Site.ID, sysToken))
            {

                using (var web = site.OpenWeb(SPContext.Current.Web.ID))
                {

                }

            }