Permissions error with SPWeb

Sometimes I should just read the Microsoft documentation. I just never know when it’s going to provide good code samples or just have a few tautologies.

At any rate, I had some code to loop through subsites that worked fine logged in as a Cotnributor, but wouldn’t work if the user had Read access.

Here’s the code I was using:

SPSite siteCollection = new SPSite(sDomain);
SPWeb site = siteCollection.AllWebs[“usergroup/someone”];
SPWebCollection subSites = site.AllWebs;

foreach (SPWeb subSite in subSites)
{
//blah
}

Now, my “Read” access user had read access to all of the subsites as well as the “usergroup/someone” site… Still I got an “Error: Access Denied” if that web part was on the page. To fix the problem:

SPSite siteCollection = new SPSite(sDomain);
SPWeb site = siteCollection.AllWebs[“usergroup/someone”];
SPWebCollection subSites = site.GetSubwebsForCurrentUser();

foreach (SPWeb subSite in subSites)
{
//blah
}

And I found the answer here: http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spsite.allwebs.aspx

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s