다음을 통해 공유


Add subsites and provide navigation functionality in MOSS 2007 - a code approach!

Creating sub sites through MOSS 2007 interface, creates a top navigation automatically. You’ll be able to reach the sub site and back to the root page with on click (actually 2 J - to and fro you see). However, this seems to be missing when we create a sub site using object model code.

Usually to create a sub site, the following code snippet is used:

ParentSite = new SPSite(strUrl);

ParentWeb = ParentSite.OpenWeb();

ChildWeb = ParentWeb.Webs.Add(strChildName, strChildName, strChildName, (uint)1033, "SridharReflector.stp", false, false);

It’s in the shortest possible form though. But a sharp intellect should be able to understand it J. There is couple of manifestations of the above code snippet, but I am using a STP file as my template here. However, the problem is: after this code runs, it creates a sub site no doubt – but the navigation to the sub site and back to the root site wouldn’t be created. Well, this was exactly what one of my customer wanted L. My *searching* skills might be bad, but believe me, I tried, tried n’ tried – end result couldn’t find an easy way to accomplish this.

However, I came up with a dirty (I mean very dirty) way to accomplish this. Code snippet is given below:

ParentSite = new SPSite(strUrl);

ParentWeb = ParentSite.OpenWeb();

ChildWeb = ParentWeb.Webs.Add(strChildName, strChildName, strChildName, (uint)1033, "SridharReflector.stp", false, false);

ChildWeb.Update();

ChildSite = new SPSite(ChildWeb.Url);

DChildWeb = ChildSite.OpenWeb();

SPNavigationNode ParentNode = new SPNavigationNode(ParentWeb.Title, ParentWeb.Url + "/default.aspx", true);

SPNavigationNode ChildNode = new SPNavigationNode(DChildWeb.Title, DChildWeb.Title + "/default.aspx");

ParentWeb.Navigation.TopNavigationBar.AddAsLast(ChildNode);

ChildWeb.Navigation.TopNavigationBar.AddAsFirst(ParentNode);

ChildWeb.Navigation.UseShared = true;

I know there’s should be an easier approach, just that I am too tired and lazy to explore any further as I have figured a lousy way of doing this. If anyone could throw more light on this, I’d really appreciate. Oh yes! I almost forgot… the above code snippet works very well. And know what, it also removes the top navigation if you delete the sub site!! Wow, that’s MOSS 2007! J

Comments

  • Anonymous
    December 04, 2007
    Hello, I'm currently running into the following pain. I want to add some items from a list to the topnavigationbar. But adding them via SPNavigation doesnt work. I am using the spweb to get the Navigation and just want add a single link at the front or the end. Do you have any idea or suggestion for me? Cheers and thanks Timmey

  • Anonymous
    March 11, 2008
    I tried searching for a little snippet code that would show how I can create a new SPWeb in a site collection.

  • Anonymous
    March 11, 2008
    I tried searching for a little snippet code that would show how I can create a new SPWeb in a site collection

  • Anonymous
    June 04, 2008
    Can you please give an example on how you can successfully delete a navigation node from the top navigation bar. I have been trying the following but always get an error (object not set to an instance of an object): Dim topNav As SPNavigationNodeCollection = web.Navigation.TopNavigationBar Dim ddlMenu As New SPNavigationNode("Site Pages", "") topNav.Delete(ddlMenu)

  • Anonymous
    June 07, 2008
    Modifying navigation elements through code is quite tricky in WSS 3.0. Here are few scenarios, I’ve tried

  • Anonymous
    July 09, 2008
    I created a new site(wss 3.0) into my site collection using the Webs.Add() method. Working fine, but in the new site breadcrumb is missing!!! any idea? When I opened the new site in SharePoint Designer, breadcrumb placeholder and asp:sitemap tags are there. But it is not appearing in the site. I created a subsite with the same template using Site Actions > Create, new site is created with breadcrumb. So whatz wrong with Webs.Add() method?

  • Anonymous
    July 09, 2008
    hi Thomas, This is the same problem I tried answering here.  SiteMap basically is used by all navigation modules including the breadcrumb.  So, if you add the navigation node as explained in this post, you should be able to see the breadcrumb as well. cheers, Sridhar

  • Anonymous
    July 11, 2008
    Thank you very much Sridhar, that worked... Thomas

  • Anonymous
    October 22, 2008
    Great post! I haven't tried this yet, but if a user doesn't have access / rights to view a particular sub site created in this approach, will he still see the link?

  • Anonymous
    December 09, 2008
    Thank you very much Sridhar, that worked... Thank you, Sushil

  • Anonymous
    December 09, 2008
    Thank you very much Sridhar, that worked...

  • Anonymous
    January 20, 2009
    Sridhar, Did you had to run stsadm to install your site template?

  • Anonymous
    February 10, 2009
    Hi, I'm creating a site using the code below. The site gets created but when i click on the site link to go into the site, it just hangs for ever. Any pointers will be appreciated. thanks using (SPSite siteCollection = new SPSite(strParentSite)) { SPWeb parentWeb                     = siteCollection.OpenWeb();                SPWebTemplateCollection Templates   = siteCollection.GetWebTemplates(Convert.ToUInt32(LOCALE_ID_ENGLISH)); SPWebTemplate siteTemplate          = Templates[siteTemplateName]; SPWeb projectWeb = parentWeb.Webs.Add(                    siteURLRequested,                    siteTitle,                    "",                    Convert.ToUInt32(LOCALE_ID_ENGLISH),                    siteTemplate,                    false, false); }

  • Anonymous
    November 05, 2009
    I'm pretty much a novice at coding. What file did you modify or create to do this magic?