We can customise the navigaiton in a sharepoint site using the WSS V 3.O navigation store , that is represented by the namespace Microsoft.Sharepoint.Navigation .
It provides classes ,properties and methods to customise the navigation of a sharepoint site .
lets see how to add a custom link to a quick launch bar /top navigation bar on activating a feature
class CustomLinkQuickLaunch : SPFeatureReceiver
{
//Get the context of the current site
SPSite oSite = SPContext.Current.Site;
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
using(SPWeb oWeb = oSite.OpenWeb())
{
//Get the tree view colllection of nodes in the quick launch bar
SPNavigationNodeCollection nodeColl=oWeb.Navigation.QuickLaunch;
//Get the tree view colllection of nodes in the top navigation bar
SPNavigationNodeCollection nodeColl=
oWeb.Navigation.TopNavigationBar;
//Add a new node to the collection giving it a titel and url , boolean value indicates whether the link is outside the current site coll or not .Since ,it is outside the current site coll we pass boolean true here
SPNavigationNode oCentralAdminNode =
new SPNavigationNode("Central Admin Home ", "url of the central admin",true);
nodeColl.AddAsLast(oCentralAdminNode);
//Now we can add a child node to the central admin link in a simialr fashion
SPNavigationNode oExtNode = new SPNavigationNode("Yahoo Home ", "http://www.yahoo.com/", true);
oCentralAdminNode.Children.AddAsFirst(oExtNode);
oWeb.AllowUnsafeUpdates = true;
oWeb.Update();
}
}
We need to delete the node from the quick launch bar/top navigation bar on feature deactivation , it will create a duplicates entry of the nodes when activated for the next time otherwise .The following code does exactly this
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
using(SPWeb oWeb = oSite.OpenWeb())
{
//Get the node collection of the quick launch or top navigation bar
SPNavigationNodeCollection nodeColl=oWeb.Navigation.QuickLaunch;
SPNavigationNodeCollection oNodeColl = oWeb.Navigation.TopNavigationBar;
oNodeColl.Delete(indexing to the node collection can be done to get a node to be deleted)
oWeb.AllowUnsafeUpdates = true;
oWeb.Update();
}
}
}
No comments:
Post a Comment