Sunday, July 12, 2009

Geting all subsites for the current user

Scenario:
I wanted to write a webpart which displays all the sub-sites user have access to. Note this is different Webpart from the one I wrote earlier here which displays all the site collection user has access to.

I thought of iterating through AllWebs property of the Site Collection and then checking for each one of them if user do have Access to the sub-web or not. But I got Access Denied while rending the webpart as a regular user with limited permission.

Solution:
GetSubwebsForCurrentUser, was the solution. SPWeb class has this method which returns the collection of subsites beneath the current site of which the current user is a member.

Code:

var subwebCollection = SPContext.Current.Web.GetSubwebsForCurrentUser();

3 comments:

Unknown August 25, 2009 at 11:00 PM  

Awesome man - I wish I had read this a couple of hours ago.

Sandeep August 25, 2009 at 11:01 PM  

its never late :-) i hope this will be helpful next time for u

sonaly September 9, 2009 at 10:09 AM  

Sandeep

In FBA login page, I want to implement a logic where after successfull login, I want to redirect user to their own site (not my site) means different login for different site. It mauy be possible user exist in mutiple site.

Using GetSubwebsForCurrentUser, how can I achieve this?

Please reply me on dishaniti@yahoo.co.in also.


Disha