Welcome to MSDN Blogs Sign in | Join | Help

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

Published Saturday, April 21, 2007 11:47 AM by sridhara
Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

Wednesday, December 05, 2007 12:49 AM by Timmey

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

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

Tuesday, March 11, 2008 8:06 PM by Paul Galvin's Blog

# Quick and Simple: Provision a Web Site Using SharePoint Object Model In C#

I tried searching for a little snippet code that would show how I can create a new SPWeb in a site collection.

Tuesday, March 11, 2008 9:02 PM by Mirrored Blogs

# Quick and Simple: Provision a Web Site Using SharePoint Object Model In C#

I tried searching for a little snippet code that would show how I can create a new SPWeb in a site collection

Wednesday, June 04, 2008 1:25 PM by Albert

# Deleting Navigation Nodes

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)

Saturday, June 07, 2008 11:41 AM by Sridhar's Blog

# Navigating through Navigation in WSS 3.0

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

Wednesday, July 09, 2008 4:10 PM by Thomas

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

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?

Wednesday, July 09, 2008 5:36 PM by sridhara

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

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

Friday, July 11, 2008 1:07 PM by Thomas

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

Thank you very much Sridhar, that worked...

Thomas

Thursday, October 23, 2008 7:52 AM by Migs

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

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?

Tuesday, December 09, 2008 7:38 PM by Sushil Maurya

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

Thank you very much Sridhar, that worked...

Thank you,

Sushil

Tuesday, December 09, 2008 7:38 PM by Sushil Maurya

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

Thank you very much Sridhar, that worked...

Tuesday, January 20, 2009 5:58 PM by Rajeev

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

Sridhar, Did you had to run stsadm to install your site template?

Tuesday, February 10, 2009 5:12 PM by ktk

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

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);

}

Friday, November 06, 2009 4:21 AM by Jeff

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

I'm pretty much a novice at coding. What file did you modify or create to do this magic?

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker