Monday, May 27, 2013
Fetch the list of all the sitecollection and subsites under the webapplication and create a sharepoint menu
public class _starter : MasterPage
{
protected Menu CustomMenu;
protected void Page_Load(object sender, EventArgs e)
{
using (SPSite site = new SPSite(SPContext.Current.Site.Url))
{
SPWebApplication webapp = site.WebApplication;
CustomMenu.Items.Clear();
BeginProcess(webapp);
}
}
public void BeginProcess(SPWebApplication webApp)
{
//Execute any logic you need to against the web application
//Iterate through each site collection
foreach (SPSite site in webApp.Sites)
{
using (SPWeb oSPWeb = site.OpenWeb())
{
MenuItem parentMenuItem = new MenuItem(oSPWeb.Title, oSPWeb.Title, "", oSPWeb.Url);
CustomMenu.Items.Add(parentMenuItem);
if (oSPWeb.Webs.Count > 0)
{
RecursiveWebCheck(oSPWeb, parentMenuItem);
}
}
}
}
private void RecursiveWebCheck(SPWeb parentoSPWeb, MenuItem parentMenuItem)
{
foreach (SPWeb web in parentoSPWeb.Webs)
{
//web.Dispose();
//if (parentoSPWeb.Webs.Count > 0)
//{
MenuItem childMenuItem = new MenuItem(web.Title, web.Title, "", web.Url);
parentMenuItem.ChildItems.Add(childMenuItem);
RecursiveWebCheck(web, childMenuItem);
//}
web.Dispose();
}
}
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment