Question

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
                        }

                    }
                });
Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top