how to add Quick Launchbar in sharepoint Programatically

 how to add  Quick Launchbar in sharepoint Programatically:

step1:
  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 =  site.RootWeb.Navigation.QuickLaunch;
                for (int j = 0; j < headnode.Length; j++)
                {
                    SPNavigationNode navNode = new SPNavigationNode(headnode[j], "http://www.google.com", false);
                    nodes.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.
 Thats it...

If you want to delet any node then:

//---------------TO DELETE NODE-----------------------------------------------------
//deletes the first and second child underneath the 7th heading on the page
                       quickLaunchNodes[6].Children.Delete(quickLaunchNodes[6].Children[0]);

If you want to know any name of the node then:

 string linkname= nodes[i].Title.ToString()

Comments

Popular posts from this blog

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

Add content type to SharePoint List/Library using REST API

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