Best way to programmatically create/maintain SharePoint Quick Launch menu

navigationsharepointsharepoint-2010

We have a solution that deploys a number of lists and pages. We wan't to create links for them on the Quick Launch menu automatically when a feature is activated.

The structure could be something like this.

  • Customers
    • Active
    • Inactive
  • Sales
    • Quotes
    • Orders

And so on. The site collection admin might add another link between the "Active" and "Inactive" links. When the feature is deactivated I don't want to remove the items, but if the feature is activated again i don't want the navigation to be added again 🙂

Is there a built in API that you can use? I know about the SPWeb.Navigation.QuickLaunch and the SPNavigationNode(Collection) structure etc. But is there another way?

Hope you can help 🙂

Best Answer

What kind of other way would you be looking for?

    public override void FeatureActivated(SPFeatureReceiverProperties properties)
    {
        SPWeb web = (SPWeb)properties.Feature.Parent; 

    // Check for an existing link to the list.
        SPNavigationNode listNode = web.Navigation.GetNodeByUrl(list.DefaultViewUrl); 

    // No link, so create one.
        if (listNode == null)
        {     
              // Create the node.
              listNode = new SPNavigationNode(list.Title, list.DefaultViewUrl);
              // Add it to Quick Launch.
              web.Navigation.AddToQuickLaunch(listNode, SPQuickLaunchHeading.Lists);
        }
    }

We have used the method above for a while and it tends to work out just fine.

If you can let me know what kind of thing you are trying to accomplish that manipulating SPWeb.Navigation wont let you do, I might be able to be of some more help

Related Topic