Tuesday, February 3, 2009

Creating a top link bar navigation in MOSS site programmatically

Hi All,

If we want to add top link navigation bar programmatically in our site, we can do it with programmatic support also.

First let’s have a look at from where we can do it from GUI.

Go to Site Settings-> under look and feel -> Navigation, in that point to Global Navigation and add Heading and write it down the address and other information. Once you are done with it, you can see new Tab coming up at top navigation bar and once you click on that, it will take you to the your provided address.

So the question is now how to do it programmatically. Here is the answer for that.

Here is a way. We have to get TopNavigationBar of the site first.

Let’s have a look at the demo code.

SPWeb objWeb = SPContext.Current.Web;

SPNavigationNodeCollection topNavigationNodes = objWeb.Navigation.TopNavigationBar;

This gives us the collection of all top navigation node of the site.

Now we want to add one more navigation bar to it.

SPNavigationNode objItem = new SPNavigationNode(“{Caption}”, “{URL}”, false);

And then finally add it to the Collection. Here we have a choices. Like simply add, addAsFirst, AddAsLast. Here we will add link as First.

topNavigationNodes.AddAsFirst(objItem);
objWeb.Update();

That’s it. You are done.

Now when you run the code, you can see one more link coming up at the top navigation bar and when you click there it will take you to the page mentioned by you in the URL parameter.

7 comments:

Sendil said...

Where I need to place this code?
Could I place the code in Master page?
If it so for each and every page its creating the link?
I need to make it dynamically based on my list
how to achieve this?

TweeZz said...

SPWeb objWeb = SPContent.Current.Web;

should be

SPWeb objWeb = SPContext.Current.Web;

SharePoint Kings said...

TweeZz, thanks to point typo.

ritu said...

Dear Thanks a lot
Ritu Ranjan
rituranjan2002@gmail.com

ritu said...

Dear Thanks a lot

Ritu Ranjan

Suresh Pydi said...

it is not working.....

Đỗ Quốc Hùng said...

I could not find how to way Update links to Top Navigation Sharepoint 2010 programmatically. so today i found it, i share it to anybody at this link
http://mstechsharing.blogspot.com/2013/04/update-links-to-top-navigation.html.

Thanks




Share your SharePoint Experiences with us...
As good as the SharePointKings is, we want to make it even better. One of our most valuable sources of input for our Blog Posts comes from ever enthusiastic Visitors/Readers. We welcome every Visitor/Reader to contribute their experiences with SharePoint. It may be in the form of a code stub, snippet, any tips and trick or any crazy thing you have tried with SharePoint.
Send your Articles to sharepointkings@gmail.com with your Profile Summary. We will Post them. The idea is to act as a bridge between you Readers!!!

If anyone would like to have their advertisement posted on this blog, please send us the requirement details to sharepointkings@gmail.com