This post is for creating a site Intranet with Path Intranet6 and subsites under the site "Intrnet" with the given siteCollectionUrl.
SPSite siteCollection = new SPSite(siteCollectionUrl);
try
{
using (SPWeb web = siteCollection.OpenWeb())
{
web.AllowUnsafeUpdates = true;
SPWebCollection webSiteCollextion = web.Webs;
try
{
SPWeb spNewWeb = webSiteCollextion["Intranet6"];
string spNewWebUrl = siteCollectionUrl + "/Intranet6";
if (spNewWeb == null || !spNewWeb.Exists)
{
spNewWeb = web.Webs.Add("Intranet6", "Intranet",
"Intranet6 from Hyper.net", 1033, "STS#0", true, false);
spNewWeb.Navigation.UseShared = false;
}
spNewWebUrl = spNewWeb.Url;
SPSite spNewSite = new SPSite(spNewWebUrl);
using (spNewWeb = spNewSite.OpenWeb())
{
//Get the Intranet Site's TopNavigationBar
SPNavigationNodeCollection topNavigationNodes = spNewWeb.Navigation.TopNavigationBar;
//Create subsites called subsite1 and subsite2
SPWeb subSite1Web = spNewWeb.Webs.Add("subsite1", "Sub Site1", "Sub Site1",
1033, "STS#0", false, false);
subSite1Web .Navigation.UseShared = true;
SPNavigationNode subSite1Node = new SPNavigationNode("SubSite1", "subsite1", false);
topNavigationNodes.AddAsFirst(subSite1Node );
SPWeb subsite2Web = spNewWeb.Webs.Add("subsite2", "Sub Site2", "Sub Site2", 1033, "STS#0", false, false);
subsite2Web .Navigation.UseShared = true;
SPNavigationNode subsite2Node = new SPNavigationNode("Sub Site2", "subsite2", false);
topNavigationNodes.AddAsFirst(subsite2Node);
SPWeb subsite3Web = spNewWeb.Webs.Add("subsite3", "Sub Site3",
"Sub Site3", 1033, "STS#0", false, false);
subsite3Web .Navigation.UseShared = true;
SPNavigationNode subsite3Node = new SPNavigationNode("Sub Site3", "subsite3", false);
topNavigationNodes.AddAsFirst(subsite3Node);
spNewWeb.Update();
spNewWeb.Close();
}
}
catch { }
}
}
catch (Exception ex)
{ }
First we check if the "Intrnet" site exists under the given SiteCollection with
SPWeb spNewWeb = webSiteCollextion["Intranet6"];
and
if (spNewWeb == null || !spNewWeb.Exists)
If the Intranet site does not exist, then we create the Site otherwise we get the SPWeb of the Intranet Site. With the Intranet SPWeb, we create the subsites sub site1, sub site2 and the sub site3.
To add the subsites in the top menu bar of the Intranet site, first you have to get the TopMenuNavigationBar of the Parent which is Intranet here. The following line of code will do that
SPNavigationNodeCollection topNavigationNodes = spNewWeb.Navigation.TopNavigationBar;
Then create each subsite navigation node and add it to the parent as the first or last nodes with AddAsLast() and AddAsFirst() methods. After creating the nodes, you have to do the Update on the spNewWeb to get the latest changes applied otherwise you have to do the IISReset manually.
You can find more information on creating topmenu navigation bars and removing them at the following link: http://www.toddbaginski.com/blog/archive/0001/01/01/how-to-programmatically-customize-site-navigation-in-wss-3.aspx
No comments:
Post a Comment