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 space
using 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

Popular posts from this blog

Upload Single/Multiple file by using the REST API and jQuery SharePoint 2013

A type named 'SP.Data. could not be resolved by the model error

Add content type to SharePoint List/Library using REST API