다음을 통해 공유


Programmatically get Navigation Settings in SharePoint Server 2010 and 2013

While writing some custom SharePoint Web Services (asmx) in a project, I encountered a situation where I needed to fetch the Navigation Settings of the current subsite and the site collection. After lots of head scratching and searching online, I found out the below pieces of code useful in getting the navigation properties for SharePoint Server 2010 and 2013.

You must include the Microsoft.SharePoint.Publishing namespace.

Note : The following applies only to SharePoint sites with publishing features enabled. Other properties can be found in the SPWeb.Navigation (web.Navigation) object however.

 using (SPSite site = new SPSite("https://yourweburl/"))
{
    using (SPWeb web = site.OpenWeb())
    {
        var publishingWeb = PublishingWeb.GetPublishingWeb(web);
        navSettings = new NavigationSettings // My Custom Model
        {
            Name = publishingWeb.Name,
            IncludePagesInCurrentNavigation = publishingWeb.Navigation.CurrentIncludePages,
            IncludePagesInGlobalNavigation = publishingWeb.Navigation.GlobalIncludePages,
            IncludeSubSitesInCurrentNavigation = publishingWeb.Navigation.CurrentIncludeSubSites,
            IncludeSubSitesInGlobalNavigation = publishingWeb.Navigation.GlobalIncludeSubSites,
            InheritCurrentNavigation = publishingWeb.Navigation.InheritCurrent,
            InheritGlobalNavigation = publishingWeb.Navigation.InheritGlobal,
            QuickLaunchEnabled = publishingWeb.Web.QuickLaunchEnabled,
            TreeViewEnabled = publishingWeb.Web.TreeViewEnabled
        };
   }
}

Useful, isn't it? You can try setting them as well using the Update() methods after assigning desired value to each of the properties to save changes to the navigation properties.