I have this code:

SPWeb web = SPContext.Current.Web;

string dummy = String.Empty;

SPWeb subwebByName = web.Webs["Example"];
dummy = subwebByName.Name;

Guid subwebId = new Guid("{5fad4b3b-3124-47d1-b268-86e322e6f2d1}");
SPWeb subwebById = web.Webs[subwebId];
dummy = subwebById.Name;

If I run this code under a user who is in the owner group it works fine.

But if I have a user who is in the visitor group the line web.Webs[subwebId] generates a ThreadAbortException and I get redirected to a page that says "Error: Access denied". The access by name works.

So why does SPWebCollection[Guid] needs more permissions than SPWebCollection[string]? And where is this documented? ;)


.NET Reflector might be useful for solving these problems.

public SPWeb this[Guid id] calls private method SPWeb.EnsureWebsData(). This method then calls SPRequest.GetSubwebsFiltered() or SPRequest.GetSubwebs(). Those methods may throw access denied exceptions.

For unknown reasons, public SPWeb this[string name] does not call EnsureWebsData().

Probably you user does not have access on all subwebs and there it fails. Or there is some other reason. Those methods work differently, so different results are expected. I would just stick with what works.

  • 1
    I would suggest ILSpy as an alternative, but the current release doesn't seem to show up property indexers yet, which is a shame. – James Love Jul 17 '11 at 19:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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