Manish
Manish

Reputation: 6286

SPNavigationNode.IsVisible property not working in sharepoint as expected

I want to hide a quick launch node in a sharepoint site, but it's not working as expected. :(

My code is as under:

                using (SPSite spSiteTest = new SPSite(serverUrl))
                {

                    using (SPWeb mySite = spSiteTest.OpenWeb())
                {
                    SPNavigationNodeCollection quickLaunchNodes = mySite.Navigation.QuickLaunch;

                    SPNavigationNode navQuickNode = new SPNavigationNode("Title", "www.stackoverflow.com", true);


                    foreach (SPNavigationNode node in quickLaunchNodes)
                    {
                        if (node.Title == navQuickNode.Title)
                        {
                            node.Url = navQuickNode.Url;
                            node.IsVisible = isVisible;
                            node.Update();
                            //mySite.Update();
                            return;
                        }
                    }   

                    quickLaunchNodes.AddAsFirst(navQuickNode);

                }
            }

Am I missing something or is it a bug?

Upvotes: 3

Views: 4446

Answers (2)

Vadim Gremyachev
Vadim Gremyachev

Reputation: 59358

How to show/hide navigation nodes using SharePoint Server Side Object Model (SSOM)

PortalNavigation Class exposes the following methods for that purpose:

Examples

How to hide site from Current navigation:

using (var site = new SPSite(siteUrl))
{
    using (var web = site.OpenWeb())
    {
        var subWeb = web.Webs["Announcements"];

        var publishingWeb = PublishingWeb.GetPublishingWeb(web);
        publishingWeb.Navigation.ExcludeFromNavigation(false,subWeb.ID);
        publishingWeb.Update();

    }
}

How to show/hide navigation nodes using SharePoint Client Side Object Model (CSOM)

It is not supported to modify navigation settings via SharePoint CSOM.

Regarding SharePoint 2013:

In SharePoint 2013 was introduced a new Microsoft.SharePoint.Client.Publishing and Microsoft.SharePoint.Client.Publishing.Navigation namespaces in CSOM API. But unfortunately it is not supported to modify navigation settings using WebNavigationSettings class since properties are exposes as a read-only.

For CSOM, you could utilize the ClientPortalNavigation.cs class that represents a CSOM counterpart for SSOM PortalNavigation Class. Follow Access and Manipulate Navigation Settings via SharePoint Client Object Model post for more details.

Examples

How to hide a page from Global navigation:

using (var ctx = new ClientContext(webUri))
{

     //Get page file
     var pageFile = ctx.Web.GetFileByServerRelativeUrl("/news/Pages/Announcements.aspx");
     ctx.Load(pageFile);
     ctx.ExecuteQuery();

     //Hide page from Global navigation
     var navigation = new ClientPortalNavigation(ctx.Web);
     navigation.ExcludeFromNavigation(true, pageFile.UniqueId);
     navigation.SaveChanges();
 }

How to hide a site from Global navigation:

 using (var ctx = new ClientContext(webUri))
 {

     //Get sub site
     var result = ctx.LoadQuery(ctx.Web.Webs.Where(w => w.Title == "Archive"));
     ctx.ExecuteQuery();
     var subWeb = result.FirstOrDefault();

     //Hide web from Global navigation
     var navigation = new ClientPortalNavigation(ctx.Web);
     navigation.ExcludeFromNavigation(true, subWeb.Id);
     navigation.SaveChanges();
 }

Upvotes: 0

Marek Grzenkowicz
Marek Grzenkowicz

Reputation: 17363

You can delete the nodes like this:

node.Delete();
mySite.Update();

or check the ExcludeFromNavigation method mentioned in this post (its author suggests that not being able to hide a navigation node by setting IsVisible to false is a SharePoint bug, too).

Upvotes: 1

Related Questions