Programmatically hide sharepoint web from default navigation

If you worked with publishing pages before you’re probably are familiar with the functionality to hide sites from navigation menu. Something like this:

hiding-feature

If you’re building a solution you probably want to script this functionality as a feature so you can call it anytime you want to provision site that will be hidden in default navigation. Here is how to achieve this using SharePoint feature framework:

1. Create a feature that will look like this:

<?xml version=”1.0″ encoding=”utf-8″?>
<Feature xmlns=”http://schemas.microsoft.com/sharepoint/
 Id=”1A59DF6F-46DF-453b-8970-6734E55B151A”
 Title=”Hide link from current navigation”
 Description=”Hides link from current navigation”
 Version=”1.0.0.0″
 Scope=”Web”
 Hidden=”FALSE”
 ActivateOnDefault=”FALSE”
 AlwaysForceInstall=”TRUE”
 ReceiverAssembly=”MyProject.SharePoint, Version=1.0.0.0, Culture=neutral, PublicKeyToken=0bcd2faaaff3d79″
 ReceiverClass=”MyProject.SharePoint.HideNav.HideNavEventReceiver”>
</Feature>

Nothing unusual above, just a feature definition in its simplest form plus reference to event receiver which will handle all of the “hiding logic”, explained in the next step.

2. Create new class inheriting from SPFeatureReceiver (in my case called HideNavEventReceiver.cs)

The FeatureActivated method will looks like this:

public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            using (SPWeb web = properties.Feature.Parent as SPWeb)
            {
                PublishingWeb pw = Microsoft.SharePoint.Publishing.PublishingWeb.GetPublishingWeb(web);
                pw.IncludeInCurrentNavigation = false;
                pw.Update();
            }
        }

In here I grab current web where my feature is activated and set its IncludeInCurrentNavigation property to false making it invisible.

That’s all! Drop me a note if you have any questions.

This entry was posted in MOSS, sharepoint, wss and tagged , , , . Bookmark the permalink.

6 Responses to Programmatically hide sharepoint web from default navigation

  1. Rosh says:

    Hi Yaroslav,

    Cool post! I was looking for something like this. I have publishing parent sites and several subsites under each of them. I want SharePoint to move a newly created subsite’s link in their parent site’s quick launch navigation under a ‘Sites’ heading. By default, SharePoint shows them at the same level as ‘Sites’, ‘Lists’ and ‘Documents’ headings in parent site quick launch bar. I have below code but I don’t know how to make a feature of it. Can you help me with that? I would really appreaciate it. Thank you. Rosh.

    using (SPSite site = new SPSite(siteURL))
    {
    using (SPWeb web = site.OpenWeb(“SiteDirectory/” + site))
    {
    int i = 0;
    int sitesIndex = 0;
    int subsiteIndex = 0;

    web.AllowUnsafeUpdates = true;
    web.Update();

    PublishingWeb pw = PublishingWeb.GetPublishingWeb(web);
    pw.Update();

    SPNavigationNodeCollection parentNodes = web.Navigation.QuickLaunch;

    for (i = 0; i < parentNodes.Count; i++)
    {
    if (parentNodes[i].Title == "Sites")
    {
    sitesIndex = i;
    }
    if (parentNodes[i].Title == "Subsite1")
    {
    subsiteIndex = i;

    SPNavigationNodeCollection childrenNodes = parentNodes[sitesIndex].Children;
    SPNavigationNode childNode = parentNodes.Navigation.GetNodeById(parentNodes[subsiteIndex].Id);
    childNode.MoveToLast(childrenNodes);
    web.Update();
    }
    }
    }
    }

  2. Hi Rosh,
    sounds like you already have the code that will do the logic and now you just need to seal inside a feature.
    You`re feature would be a web scoped feature with event receiver defined something like this:

    The you would create a class named HideNavEventReceiver or something that looks the same as in your feature XML definition file above. Your class inheriting from SPFeatureReceiver would have the following code (pretty much your code wrapped in a feature):

    public override void FeatureActivated(SPFeatureReceiverProperties properties)
    {
    using (SPWeb web = properties.Feature.Parent as SPWeb)
    {
    int i = 0;
    int sitesIndex = 0;
    int subsiteIndex = 0;

    web.AllowUnsafeUpdates = true;
    web.Update();

    PublishingWeb pw = PublishingWeb.GetPublishingWeb(web);
    pw.Update();

    SPNavigationNodeCollection parentNodes = web.Navigation.QuickLaunch;

    for (i = 0; i < parentNodes.Count; i++)
    {
    if (parentNodes[i].Title == "Sites")
    {
    sitesIndex = i;
    }
    if (parentNodes[i].Title == "Subsite1")
    {
    subsiteIndex = i;

    SPNavigationNodeCollection childrenNodes = parentNodes[sitesIndex].Children;
    SPNavigationNode childNode = parentNodes.Navigation.GetNodeById(parentNodes[subsiteIndex].Id);
    childNode.MoveToLast(childrenNodes);
    web.Update();
    }
    }
    }
    }

    Let me know if thats what you were looking for.

  3. Jennifer says:

    Hi Yaroslav,

    I need your help in something like this. I have developed sitemap webpart using object model which is working fine. But recently some of the sites were made as hidden in the navigation. Here the requirement is to avoid those hidden sites in the sitemap webpart. Since then i have been searching a lot.your post seems something relevant to it.

    Any suggestions/help would be highly appreciated.

  4. If you don`t want to display hiden nodes in your webpart, use something like this:
    SPWeb web = SPContext.Current.Web;
    PublishingWeb pw = Microsoft.SharePoint.Publishing.PublishingWeb.GetPublishingWeb(web);
    if (!pw.IncludeInCurrentNavigation)
    {
    …..
    your code to hide relevant webpart nodes
    ……
    }

  5. Jennifer says:

    Thanks a lot yaroslav.
    This helped me to solve my issue.
    @Yaroslav Pentsarskyy

  6. That’s great to hear, Good luck!