Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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

share|improve this question
    
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)) { ... } } ? –  Daniel Ziga Nov 30 '11 at 8:23
add comment

3 Answers

up vote 4 down vote accepted

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();
      }
    }
  }
}
share|improve this answer
    
This method is preferred over the RWEP, we don't have to impersonate the user just open the SPSite as another user... –  Wictor Wilen MCA MCM MVP Nov 30 '11 at 8:45
    
Also, check this blog post for more information: matchpointcommunity.com/blog/Posts/37/… –  Matthias Dec 7 '11 at 14:19
add comment

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 })

share|improve this answer
    
I updated my answer to show you memory leaks. –  Alexander Nov 30 '11 at 8:30
    
thanks, I will give it a go, any link where I can read about this memory leak? –  John Nov 30 '11 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 Nov 30 '11 at 8:49
add comment

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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