Social Icons

twitter google plus linkedin rss feed

Pages

17.9.10

Lambdas, Method Extensions and RunWithElevatedPrivileges

Nowadays it's not a problem for anyone to create a new site as an administrator regardless of the loged user, but I'd like to suggest a function that seems useful to me:
/// 
        /// Returns a NEW site with full permissions
        /// 
        public static SPSite OpenSiteWithElevatedPrivileges(this SPSite site)
        {
            SPSite newSite = null;

            SPSecurity.RunWithElevatedPrivileges(() => newSite = new SPSite(site.ID));

            return newSite;
        }
Looks easy, doesn't it? I like doing
SPSite site = SPContext.Current.Site.OpenSiteWithElevatedPrivileges();
What do you think?