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.
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, stringitem, 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(newSPNavigationNode(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(newSPNavigationNode(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(newSPNavigationNode(item, url, true));
else
nodeItem.Url = url;
nodeItem.Update();
nodeHeader.Update();
}
}
}
}
}
No comments:
Post a Comment