This is the below code which I used to get the web reference using the title of the web site. The problem is the current user is not a member of all sub site so I'm getting access denied error.

User A have rights to: Web1, Web2 (no problem with code)

User B have rights to: Web1 (problem with code because he doesn't have rights to web2)

int projectid = GetProjectID(SPContext.Current.Web.Title, Url);

string projecttitle = SPContext.Current.Web.Title;

var nodename = currentWeb.ChildNodes.Cast<SiteMapNode>().FirstOrDefault(node =>
node.Title == projecttitle);
  • On what row is the access denied coming from? If it is in GetProjectID method, please show us from what row in that.
    – Jussi Palo
    Feb 7, 2012 at 7:24
  • var nodename = currentWeb.ChildNodes.Cast().FirstOrDefault(node => node.Title == projecttitle); Feb 7, 2012 at 8:09
  • Added an answer, but is there a reason (performance, perhaps) why you are using SiteMapNode)?
    – Jussi Palo
    Feb 7, 2012 at 8:41

2 Answers 2




to get only subwebs current user has access to.

If you want to get all sites even if user doesn't have permissions to access them, use SPSecurity.RunWithElevatedPrivileges:

    using (SPSite site = new SPSite(web.Site.ID))
    // implementation details omitted

Use SPSecurity.RunWithElevatedPrevileges. The code inside the specified delegate will then run under the identity of the current app pool user.

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.