Adding Items to your Quick Launch or Top Navigation Programmatically!


I had issues trying to research and find a way to add to my Quick Launch menu or my Top Navigation menu, programmatically.  This was a huge challenge.  One of my employees found this link: http://www.anmolrehan-sharepointconsultant.com/2011/09/sharepoint-working-with-quicklaunch.html.  This was something we were able to start out with to add items to the quick launch.  I further modified it as shown below, and hope this is something that you can use.

image

 

using System;

using System.Web.UI;

using System.Linq;

using Microsoft.SharePoint;

using Microsoft.SharePoint.Navigation;

 

namespace SPQuickLaunchAddItemWebPart.VisualWebPart1

{

    public partial class VisualWebPart1UserControl : UserControl

    {

        protected void Page_Load(object sender, EventArgs e)

        {

            CleanUpQuickLaunch(“My Header”);

            AddQuickLaunchItem(“My Header”, http://www.google.com/q=stuff”, “My Link Here”, http://www.google.com”);

 

            CleanUpTopNavigationBar(“My Header”);

            AddTopNavigationBarItem(“My Header”, http://www.google.com/q=stuff”, “My Link Here”, http://www.google.com”);

            AddTopNavigationBarItem(“My Header”, http://www.google.com/q=stuff”, “My Link Here1”, http://www.google.com”);

        }

 

        public static void CleanUpQuickLaunch(string header)

        {

            using (SPSite site = SPContext.Current.Site)

            {

                using (SPWeb web = site.OpenWeb())

                {

                    web.AllowUnsafeUpdates = true;

                    SPNavigationNodeCollection quickLaunch = web.Navigation.QuickLaunch;

 

                    // try to get quick launch header

                    SPNavigationNode nodeHeader = quickLaunch.Cast<SPNavigationNode>().Where(n => n.Title == header).FirstOrDefault();

 

                    //if header not found remove it

                    if (nodeHeader != null)

                    {

                        quickLaunch.Delete(nodeHeader);

                    }

 

                }

            }

        }

 

        public static void CleanUpTopNavigationBar(string header)

        {

            using (SPSite site = SPContext.Current.Site)

            {

                using (SPWeb web = site.OpenWeb())

                {

                    web.AllowUnsafeUpdates = true;

                    SPNavigationNodeCollection topNavigation = web.Navigation.TopNavigationBar;

 

                    // try to get quick launch header

                    SPNavigationNode nodeHeader = topNavigation.Cast<SPNavigationNode>().Where(n => n.Title == header).FirstOrDefault();

 

                    //if header not found remove it

                    if (nodeHeader != null)

                    {

                        topNavigation.Delete(nodeHeader);

                    }

                }

            }

        }

 

        public static void AddQuickLaunchItem(string header, string headerURL, string item, string url)

        {

            using (SPSite site = SPContext.Current.Site)

            {

                using (SPWeb web = site.OpenWeb())

                {

                    web.AllowUnsafeUpdates = true;

                    SPNavigationNodeCollection quickLaunch = web.Navigation.QuickLaunch;

 

                    // try to get quick launch header

                    SPNavigationNode nodeHeader = quickLaunch.Cast<SPNavigationNode>().Where(n => n.Title == header).FirstOrDefault();

 

                    //if header not found create it

                    if (nodeHeader == null)

                        nodeHeader = quickLaunch.AddAsFirst(new SPNavigationNode(header, headerURL, true));

                    nodeHeader.Update();

 

                    //try to get node item under header

                    SPNavigationNode nodeItem = nodeHeader.Children.Cast<SPNavigationNode>().Where(n => n.Title == item).FirstOrDefault();

 

                    //If item not found under heading then create it

                    if (nodeItem == null)

                        nodeItem = nodeHeader.Children.AddAsLast(new SPNavigationNode(item, url, true));

                    else

                        nodeItem.Url = url;

 

                    nodeItem.Update();

                    nodeHeader.Update();

                }

            }

        }

 

        public static void AddTopNavigationBarItem(string header, string headerURL, string item, string url)

        {

            using (SPSite site = SPContext.Current.Site)

            {

                using (SPWeb web = site.OpenWeb())

                {

                    web.AllowUnsafeUpdates = true;

                    SPNavigationNodeCollection topNavBar = web.Navigation.TopNavigationBar;

 

                    // try to get quick launch header

                    SPNavigationNode nodeHeader = topNavBar.Cast<SPNavigationNode>().Where(n => n.Title == header).FirstOrDefault();

 

                    //if header not found create it

                    if (nodeHeader == null)

                        nodeHeader = topNavBar.AddAsFirst(new SPNavigationNode(header, headerURL, true));

                    nodeHeader.Update();

 

                    //try to get node item under header

                    SPNavigationNode nodeItem = nodeHeader.Children.Cast<SPNavigationNode>().Where(n => n.Title == item).FirstOrDefault();

 

                    //If item not found under heading then create it

                    if (nodeItem == null)

                        nodeItem = nodeHeader.Children.AddAsLast(new SPNavigationNode(item, url, true));

                    else

                        nodeItem.Url = url;

 

                    nodeItem.Update();

                    nodeHeader.Update();

                }

            }

        }

    }

}

 

 

Categories: SharePoint or Custom Development | Leave a comment

Post navigation

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: