6

We're developing a public facing SharePoint site so it have anonymous access. To be able to run custom web parts that access lists/libraries we need to run with elevated privileges which works fine, no problem.

However, we have custom user control that displays the current sites sub sites and in the code we use .Webs which seems to be a problem (we get prompted for credentials on the page and when we debug).

private void LoadSubsites()
{
    SPSecurity.RunWithElevatedPrivileges(delegate()
    {
        using (SPWeb web = SPContext.Current.Site.OpenWeb(SPContext.Current.Web.ServerRelativeUrl))
        {
            var sites = web.Webs.Select(w => new { Title = w.Title, Url = w.Url });
            if (sites != null)
            {
                rptSubsites.DataSource = sites;
                rptSubsites.DataBind();
            }
        }
    });
}

What should I do to enable anonymous users to access pages with this user control?

Thanks in advance

1
  • Have you tried wrapping it in a using (SPSite site = new SPSite(SPContext.Current.Site.ID)) { using (SPWeb web = site.OpenWeb(SPContext.Current.Web.ID)) { ... } } ?
    – user2536
    Commented Nov 30, 2011 at 8:23

3 Answers 3

5

You could open the site with the SystemAccount token. It is more recommended than using SPSecurity.RunWithElevatedPrivileges.
I would do it like this:

private void LoadSubsites()
{
  using (SPSite site = new SPSite(SPContext.Current.Web.Url, SPUserToken.SystemAccount)
  {
    using (SPWeb web = site.OpenWeb())
    {
      var sites = web.Webs.Select(w => new { Title = w.Title, Url = w.Url });
      if (sites != null)
      {
        rptSubsites.DataSource = sites;
        rptSubsites.DataBind();
      }
    }
  }
}
2
2

First at all you should open new site context

 SPSecurity.RunWithElevatedPrivileges(delegate()
    {
        using (SPSite site = new SPSite(SPContext.Current.Site.ID))
        {

        using (SPWeb web = site.OpenWeb(SPContext.Current.Web.ID))
        {
            var sites = web.Webs.Select(w => new { Title = w.Title, Url = w.Url });
            if (sites != null)
            {
                rptSubsites.DataSource = sites;
                rptSubsites.DataBind();
            }
          }}
    });

The second issue in your code is: you have memory leaks. var sites = web.Webs.Select(w => new { Title = w.Title, Url = w.Url }). To resolve it you can use web.Webs.WebsInfo.Select(w => new { Title = w.Title, Url = w.ServerRelativeUrl })

3
  • I updated my answer to show you memory leaks.
    – Alexander
    Commented Nov 30, 2011 at 8:30
  • thanks, I will give it a go, any link where I can read about this memory leak?
    – John
    Commented Nov 30, 2011 at 8:41
  • 1
    The memory leak Alexander mentioned is pointed here: msdn.microsoft.com/en-us/library/…. You are iterating over the webs of SPWeb.Webs, which each of it has to be disposed after accessing it.
    – Matthias
    Commented Nov 30, 2011 at 8:49
1

You cannot use current context inside Elevated Privileges.

SPContext.Current object is created with security context of current logged in user. So you will get access denied error.

Use the following code -

 SPSite site = SPContext.Current.Site;  
SPWeb web = SPContext.Current.Web;  

SPSecurity.RunWithElevatedPrivileges(delegate() {  
  using (SPSite secureSite = new SPSite(site.ID)) {  
    using (SPWeb secureWeb= secureSite.OpenWeb(web.ID)) {  
     ListUSers(secureWeb);  
    }  
  }  
});  

Code referred from this msdn post

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.