SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I followed this article to display sub-sites on site collection's home page. I works fine when user have contribute access. But user with read or view only gets Access Denied. What are my options so I can be viewable for everyone with any permission(s).

share|improve this question
up vote 2 down vote accepted

It worked after i used SPSecurity.RunWithElevatedPrivileges(delegate() to wrap it around.

share|improve this answer

One thing to check before resorting to runwithelevatedprivileges is that the page you're showing to the user is both 'published' and 'approved' otherwise strange things can happen with permissions

share|improve this answer
Good point HC. I will go ahead and adhere with Anders solution and see how it plays after launching to dev and test. Thanks HC – Bob Dinero Nov 10 '10 at 14:06

A couple of things to note:

When you wrap your code with RWEP you will override the normal security trimming, and hence when users click the sites they are not allowed to see (this is why the code fails with access denied, since you are not allowed to see all elements in the SPWebCollection). A better alternative is to use SPWeb.GetSubWebForCurrentUser.

Also the code you point to have memory leak issues: you should dispose SPSite and SPWeb controls created by yourself. Find a good guidance here on how to correctly dispose webs and sites.

Also beware that if you have alot of sub sites touching SPWebCollection (rootSite.Webs) could be an expensive operation.

share|improve this answer
Thanks Anders. I implemented the change in the code but haven't deployed to dev or test to see whether is going to work or not. – Bob Dinero Nov 10 '10 at 14:05
im not saying it doesnt work, im saying your production environment will leak SPRequest objects until memory is full and it will then recycle application pool, slowing your site down as it goes. Also it is unnecessary to use RWEP as you have a method for what you want to achieve (GetSubWebForCurrentUser) – Anders Rask Nov 10 '10 at 15:56

Your Answer


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