0

There is list present on SiteCol1\RootWeb. UserA does not have permission on SiteCol1. So we are using RWEP. We read that we need to create new objects for site and web. However, foll. code works, I need to know if using (SPWeb rootWeb = site.OpenWeb()) automaticalley calls new keyword.

Or what is the way to create new object of spweb in this case?

SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                    using (SPSite site = new SPSite(SPContext.Current.Site.WebApplication.Sites[0].RootWeb.Url))
                    {
                        using (SPWeb rootWeb = site.OpenWeb())
                        {
                           //code to fetch data from list present in root web
                        }

                    }
                });
variable
  • 4,473
  • 13
  • 75
  • 139

1 Answers1

1

The reason why this works is that you have already created a new SPSite object inside the RWEP and hence SPWeb returned by using SPWeb rootWeb = site.OpenWeb() correctly gives the elevated web. The issue happens if you try to use previously created SPSite or SPWeb objects inside the delegate. Note that in your case even SPWeb rootWeb = site.RootWeb should also work. See this for more information: http://blogs.technet.com/b/sharepointdevelopersupport/archive/2013/03/13/using-spsite-and-spweb-objects-with-runwithelevatedprivileges-don-t-cross-the-borders.aspx

Nadeem Yousuf-AIS
  • 18,707
  • 4
  • 28
  • 59
  • would using SPWeb=SPContext.Current.Web within the RWEP also be OK if we create a new site obejct? – variable Apr 14 '14 at 10:41
  • No, that is not right. The new web object needs to be retrieved from the new elevated site object. If you get SPWeb object using SPContext, then it will be not run with elevated privileges. – Nadeem Yousuf-AIS Apr 14 '14 at 10:46