Wednesday, January 19, 2011

Programmatically hide pages from Navigation

Suppose you are creating a page using Features, now you want that page to be hidden from the navigation when you create it. How will you go about it---

Create a Feature Receiver for that page and enter the below code at Feature activated event.

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
PublishingWeb publishingWeb = null;
var objWeb = properties.Feature.Parent as SPWeb;
try
{
if (PublishingWeb.IsPublishingWeb(objWeb))
{
publishingWeb = PublishingWeb.GetPublishingWeb(objWeb);
// get the pages collection
PublishingPageCollection pages = publishingWeb.GetPublishingPages();

if (pages[publishingWeb.Url + "/Pages/chat.aspx"] != null)
{
//page exists hide it
PublishingPage page = pages[publishingWeb.Url + "/Pages/chat.aspx"];
page.IncludeInCurrentNavigation = false;
page.IncludeInGlobalNavigation = false;
}
}
}
catch (Exception ex)
{
using (StreamWriter w = new StreamWriter("C:\\log.txt"))
{
w.WriteLine(ex.ToString());
}
}
}

No comments:

Post a Comment