Programmatically Hide Pages From Navigation When Added To List

8 Sep

This has become a fairly frequent request from customers using SharePoint 2007 for Web Content Management. 

Specifically, in a MOSS publishing site, it is requested that links are not automatically added to navigation when the pages are created in the pages library.  Instead, the page should be marked as 'hidden' in the navigation automatically when added to the pages library.  This applies to either Quick Launch, or the Top Navigation (or both).

Typically this is because the governance of the site involves a different person managing the navigation than who may be managing the pages, thus even when a page is added it should not show in the nav until unhidden by another party.

The easiest way I'm aware of to achieve this is with a simple event handler on the pages libraries to hide the page once it is added.

The PublishingPage object will wrap an SPListItem for us, and give us access to the navigation settings.

Here is a simple implementation of this event receiver.  Note that it references Microsoft.SharePoint.dll and Microsoft.SharePoint.Publishing.dll

<span class="kwrd">using</span> System;
<span class="kwrd">using</span> System.Collections.Generic;
<span class="kwrd">using</span> System.Text;
<span class="kwrd">using</span> Microsoft.SharePoint;
<span class="kwrd">using</span> Microsoft.SharePoint.Publishing;
<span class="kwrd">namespace</span> PageNavigationEvent
{
<span class="kwrd">public</span> <span class="kwrd">class</span> HandlePageAdd : SPItemEventReceiver    
{
<span class="kwrd">public</span> <span class="kwrd">override</span> <span class="kwrd">void</span> ItemAdded(SPItemEventProperties properties)
{
<span class="kwrd">base</span>.ItemAdded(properties);
<span class="kwrd">try</span>
{
<span class="kwrd">this</span>.DisableEventFiring();
<span class="kwrd">if</span> (PublishingPage.IsPublishingPage(properties.ListItem))
{
PublishingPage newPage;
newPage = PublishingPage.GetPublishingPage(properties.ListItem);
newPage.IncludeInCurrentNavigation = <span class="kwrd">false</span>;
newPage.IncludeInGlobalNavigation = <span class="kwrd">false</span>;
}
}
<span class="kwrd">catch</span>
{
<span class="rem">// do something clever</span>
}
<span class="kwrd">finally</span>
{
<span class="kwrd">this</span>.EnableEventFiring();
}
}
}
}

 

For initial deployment and testing, I recommend you try out the fantastic Event Receiver Manager which is simple to install and will allow you to graphically add your receiver to a specific pages library without building a feature.

For proper deployment you will want to create a feature, which can install the receiver on all pages lists.  You can find instructions on doing this here.

Tweet about this on TwitterShare on Google+Share on FacebookShare on RedditEmail this to someoneShare on LinkedInShare on StumbleUpon

Leave a Reply

Your email address will not be published. Required fields are marked *