Hiding MOSS Pages From Current Navigation Programatically

I have recently had the task to hide a set of MOSS Pages in a Publishing site upon activation of a Site Feature.  In the post we will explore the code I used to make this happen along with the use of the FeatureActivated and FeatureDeactivating events in Microsoft.SharePoint.SPFeatureReceiver Class. 

The method to hide the pages is pretty straight forward, the method below takes a listing of pages in a semicolon delimited string and determines if the page is in the listing.  if so it then uses the SharePoint API to update the pages IncludeInCurrentNavigation Property.  Performance may be better if you use the IncludeInNavigation or ExcludeFromNavigation methods when showing or hiding several children of the same parent.

static public void HidePages(SPWeb web, string pages)

        {

            string[] pageList = pages.Split(';');

            Boolean inList = true;

            string pagename;

            if (PublishingWeb.IsPublishingWeb(web))

            {

                PublishingWeb pw = PublishingWeb.GetPublishingWeb(web);

                PublishingSite site = new PublishingSite(web.Site);

                PublishingPageCollection ppc = pw.GetPublishingPages();

               

                foreach (PublishingPage pp in ppc)

                {

                   

                    foreach (string p in pageList)

                    {

                        pagename = String.Format("Pages/{0}", p);

                        if (String.Compare(pp.Url, pagename,true) == 0)

                        {

                            inList = true;

                        }

                    }

                

                    if (inList)

                    {

                       

                        pp.CheckOut();

                        pp.IncludeInCurrentNavigation = false;

                        pp.Update();

                        pp.CheckIn("none");

                    }

                    inList = false;

                   

                }

            }

           

        }