how to add Top navigaiton bar link in sharepoint Programatically
how to add Top Navigation link in sharepoint Programatically:
step 1:
First you need to add the following name spaceusing Microsoft.SharePoint;
using Microsoft.SharePoint.Navigation;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Publishing;
step 2:
And next include the following code in your program
protected void Page_Load(object sender, EventArgs e)
{
using (var site = new SPSite(SPContext.Current.Site.Url))
{
var rootPubweb = PublishingWeb.GetPublishingWeb(site.RootWeb);
var subwebs = rootPubweb.GetPublishingWebs();
site.RootWeb.AllowUnsafeUpdates = true;
string[] headnode = { "link1", "link2"};
SPNavigationNodeCollection nodes_t = site.RootWeb.Navigation.TopNavigationBar;
for (int j = 0; j < headnode.Length; j++)
{
SPNavigationNode navNode = new SPNavigationNode(headnode[j], "http://www.google.com", false);
nodes_t.AddAsFirst(navNode);
}
}
step 3:
Finally deploy the web part. and insert the web part in your site& reload the page. this link will appear in your site.
Comments
Post a Comment