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

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);
share|improve this question
    
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 '12 at 7:24
    
var nodename = currentWeb.ChildNodes.Cast().FirstOrDefault(node => node.Title == projecttitle); –  MarsMan Feb 7 '12 at 8:09
    
Added an answer, but is there a reason (performance, perhaps) why you are using SiteMapNode)? –  Jussi Palo Feb 7 '12 at 8:41
add comment

2 Answers

up vote 0 down vote accepted

Use

SPWeb.GetSubwebsForCurrentUser

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:

SPSecurity.RunWithElevatedPrivileges(delegate()
{
    using (SPSite site = new SPSite(web.Site.ID))
    {
    // implementation details omitted
    }
});
share|improve this answer
add comment

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

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.