- This method is introduced in SharePoint 2010 and is available in Sandboxed Solutions also.
- This returns a SPWebCollection object.
- Namespace: Microsoft.SharePoint
- This method returns the collection of child sites of the current site based on the user permission. Means this will return all the sub site which the user has permission to access.
- Code sample:
System.Text.StringBuilder sb = new System.Text.StringBuilder();
SPWeb currentWeb = SPContext.Current.Site.RootWeb;
foreach (SPWeb web in currentWeb.GetSubwebsForCurrentUser())
if (web.Webs != null && web.Webs.Count > 0)
sb.Append("Site Title " + web.Title + " Site URL" + web.Url);
The above code will add the current web title and url to the StringBuilder.
- Benifit of this method is if your site collection contains some sub site which the logged in user does not have permission, then it will not through any exception rather it will return null.