Wednesday, August 27, 2008

Add Quick Links using Custom Code

try
{
// Get the Site Collection using SPSites
SPSite quickLaunchSite = new SPSite("Top Level Site Name");
using (quickLaunchSite)
{
// Sharepoint have high security Code.
// So we can use to AllowUnsafeupdates keyword to overwrite the security. quickLaunchSite.AllowUnsafeUpdates = true;
//Find the Particular site and open the site using SPWeb
SPWeb quickLaunchWeb = quickLaunchSite.OpenWeb();
using (quickLaunchWeb)
{
quickLaunchWeb.AllowUnsafeUpdates = true;
// Read the Quick Launch Bar Nodes using SPNavigationNodeCollection SPNavigationNodeCollection quickLaunchNodes = quickLaunchWeb.Navigation.QuickLaunch;
// Read the Internal Quick Launch bar Manu Items using SPNavigationNode SPNavigationNode internalMenuItem = new SPNavigationNode("Top Level Sites", "", false);
// Add First Menu to the Quick Launch Bar
quickLaunchNodes.AddAsFirst(internalMenuItem);
// Add Tech Support Sub Manu items to the Quick Launch Bar
SPNavigationNode externalSubMenuItem1 = new SPNavigationNode("IT", "http://ServerName/sites/TechSupport/default.aspx", true);
quickLaunchNodes[0].Children.AddAsFirst(externalSubMenuItem1);
// Add Sales Opreation Sub Manu items to the Quick Launch Bar
SPNavigationNode externalSubMenuItem2 = new SPNavigationNode("HR", "HR SITE NAME", true);
quickLaunchNodes[0].Children.AddAsFirst(externalSubMenuItem2);
// Add Tech Engineering Sub Manu items to the Quick Launch Bar SPNavigationNode externalSubMenuItem3 = new SPNavigationNode("MARKETING", "MARKETING SITE NAME", true);
quickLaunchNodes[0].Children.AddAsFirst(externalSubMenuItem3);
// Add IT Sub Manu items to the Quick Launch Bar
SPNavigationNode externalSubMenuItem4 = new SPNavigationNode("DEVELOPMENT", "DEVELOPMENT SITE NAME", true);
quickLaunchNodes[0].Children.AddAsFirst(externalSubMenuItem4);
// Add Marketing Sub Manu items to the Quick Launch Bar
SPNavigationNode externalSubMenuItem5 = new SPNavigationNode("TESTING", "TESTING SITE NAME", true);
quickLaunchNodes[0].Children.AddAsFirst(externalSubMenuItem5);
// Add Sales Sub Manu items to the Quick Launch Bar
SPNavigationNode externalSubMenuItem6 = new SPNavigationNode("SUPPORTS", "SUPPORTS SITE NAME", true);
quickLaunchNodes[0].Children.AddAsFirst(externalSubMenuItem6);
// Add Senior Management Sub Manu items to the Quick Launch Bar SPNavigationNode externalSubMenuItem7 = new SPNavigationNode("MANAGEMENT", "MANAGEMENT SITE NAME", true);
quickLaunchNodes[0].Children.AddAsFirst(externalSubMenuItem7);
// Add Tech Support Sub Manu items to the Quick Launch Bar
SPNavigationNode externalSubMenuItem8 = new SPNavigationNode("USERS", "USERS SITE NAME", true);
quickLaunchNodes[0].Children.AddAsFirst(externalSubMenuItem8);
//Finally Update menu and sub menus to Quick Launch Bar
quickLaunchWeb.Update();
}
}
}
catch (SPException x)
{
Response.Write(x.Message.ToString());
}

No comments: