Tuesday, May 10, 2016

Create Headers in SharePoint

Create Headers in SharePoint


        public void CreateMyHeaderNLink(String SelectedLibrary, bool newLinkUnderHeader)
        {            
            SPNavigationNode newHeading = null;
            SPList listname = null;
            SPNavigationNodeCollection nodeColl = null;
            SPNavigationNode myDATNode = null;
            webElevated.AllowUnsafeUpdates = true;
            bool availMyDataRoomHeading = false;
            try
            {
                listname = webElevated.Lists.TryGetList(SelectedLibrary);
                PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(webElevated);
                nodeColl = publishingWeb.Navigation.CurrentNavigationNodes;
            }
            catch (Exception ex)
            {
                try
                {
                    activateWebPublishingFeature();
                }
                catch (Exception ex1)
                {
                    CreateMyHeaderNLink(SelectedLibrary, true);
                }
            }
            // Create or Delete heading and link
            if (nodeColl != null)
                foreach (SPNavigationNode myNode in nodeColl)
                {
                    if (myNode.Title == "My Headers")
                    {
                        availMyDataRoomHeading = true;
                        myDATNode = myNode;
                        break;
                    }
                }
            if (listname != null)
            {
                if (!availMyDataRoomHeading)
                {
                    // create
                    string DTROwnerUrl = listname.ParentWeb.Url.ToString() + "/_layouts/15/xxx/yyy.aspx";
                    newHeading = SPNavigationSiteMapNode.CreateSPNavigationNode("My Headers", DTROwnerUrl, NodeTypes.Heading, nodeColl);
                    webElevated.Update();
                    myDATNode = newHeading;
                }
                if (newLinkUnderHeader)
                {  // add to link existing header
                    try
                    {
                        bool isNodelinkExist = false;
                        if (myDATNode != null)
                        {
                            int lnkCount = myDATNode.Children.Count;
                            while (lnkCount != 0)
                            {
                                if (myDATNode.Children[lnkCount - 1].Title == listname.Title.ToString())
                                {
                                    isNodelinkExist = true; //Node exist
                                    break;
                                }
                                lnkCount--;
                            }

                            if (!isNodelinkExist)
                            {
                                SPNavigationNode listNode = new SPNavigationNode(listname.Title, listname.DefaultViewUrl);
                                if (myDATNode != null)
                                    myDATNode.Children.AddAsFirst(listNode);
                                webElevated.Update();
                            }
                        }
                    }

                    catch (Exception ex) { }
                }
            }
            webElevated.AllowUnsafeUpdates = false;

        }
        public void activateWebPublishingFeature()
        {           
            try
            {
                webElevated.AllowUnsafeUpdates = true;
                //Activate the publishing feature at the web level
                SPFeatureCollection wFeatureCollect = webElevated.Features;
                wFeatureCollect.Add(new Guid("94C94CA6-B32F-4da9-A9E1F3D343D7ECB"), true);
                webElevated.AllowUnsafeUpdates = false;
            }
            catch (Exception ex)
            {
                try
                {
                    SPWeb webRoot = webElevated.Site.RootWeb;
                    webRoot.Site.AllowUnsafeUpdates = true;
                    //Activate the publishing feature at the site collection level
                    SPFeatureCollection sFeatureCollect = webRoot.Features;
                    sFeatureCollect.Add(new Guid("F6924D36-2FA8-4f0b-B16D-06B7250180FA"), true);
                    webRoot.AllowUnsafeUpdates = false;
                }

                catch (Exception ex1)
                {
                   // ("Error Occured! : Publishing feature activation" + ex1.Message);
                }
            }
        }



--------------------------------------------------------------
   public void clearnavBar(string siteUrl)
        {
            using (SPSite site = new SPSite(siteUrl))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    web.AllowUnsafeUpdates = true;
                    PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(web);
                    SPNavigationNodeCollection nodeColl = publishingWeb.Navigation.CurrentNavigationNodes;              
                  

                  int lnkCount = nodeColl.Count;
                  while (lnkCount != 0)
                  {
                      if (nodeColl[lnkCount - 1].Title != "My Headers")
                      {
                          nodeColl[lnkCount - 1].Delete();
                      }
                      lnkCount--;
                  }                 
                    web.Update();
                    web.AllowUnsafeUpdates = false;
                }
            }
        }

------------------------------------------------

  public static void NavigationOderBy()
        {
            //Structural Navigation: Sorting  
            PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(webElevated);
            publishingWeb.Navigation.OrderingMethod = OrderingMethod.Automatic;
            publishingWeb.Navigation.AutomaticSortingMethod = AutomaticSortingMethod.Title;

        }

No comments:

Post a Comment