zoukankan      html  css  js  c++  java
  • Programmatically set navigation settings in SharePoint 2013

    When working on a project where I needed to set the navigation for newly created sites programmatically, I had a hard time finding the correct properties and methods in the SharePoint object model to change all the navigation settings. After some researching I found that you need no less than 3 different objects representing the navigation of a website to be able to modify all the settings. These examples apply to SharePoint sites with publishing features enabled (navigation options differ on non-publishing sites), and just using the PublishingWeb.Navigation and WebNavigationSettings objects were sufficient for me. Other properties can be found in the SPWeb.Navigation object however.

    These are the objects containing navigation properties:

    • web.Navigation
    • publishingWeb.Navigation
    • webNavigationSettings

    Here are examples of how you instantiate these objects:

    // Web Navigation
    using (var site = new SPSite("http://somesite"))
    using (var web= site.OpenWeb())
    {  var navigation = web.Navigation;
    }
    
    // PublishingWeb Navigation
    var pubWeb = PublishingWeb.GetPublishingWeb(web);
    
    // WebNavigationSettings
    var webNavigationSettings = new WebNavigationSettings(web);

    Once instantiated, you will be able to modify the navigation using these objects. I will show some examples of how to set different options on the Navigation settings page on a publishing site through code.

    Select type of navigation

    There are two navigations. The Global navigation (also called Top navigation) and the Current navigation (also called left navigation or quicklaunch). Both of these can be set to different to use different sources. They can inherit from their parent (if a subsite only), use managed navigation, or structural navigation. All you need to do is select the webNavigationSettings object, choose which navigation to set, and select a source for that navigation.

    webNavigationSettings.GlobalNavigation.Source = StandardNavigationSource.TaxonomyProvider;
    webNavigationSettings.GlobalNavigation.Source = StandardNavigationSource.InheritFromParentWeb;
    webNavigationSettings.GlobalNavigation.Source = StandardNavigationSource.PortalProvider;
    webNavigationSettings.GlobalNavigation.Source = StandardNavigationSource.Unknown;
    webNavigationSettings.CurrentNavigation.Source = StandardNavigationSource.Unknown;

    TaxonomyProvider is Managed Metadata Navigation, PortalProvider is structural navigation, InheritFromParentWeb is self-explanatory, and Unknown leaves no radiobutton selected.

    Show subsites and pages

    To change these options you need to use the PublishingWeb object.

    pubWeb.Navigation.CurrentIncludeSubSites = false;
    pubWeb.Navigation.CurrentIncludePages = false;
    pubWeb.Navigation.CurrentDynamicChildLimit = 20;

    Managed navigation: Page settings

    To change the values of these two checkboxes, use the WebNavigationSettings object again.

    webNavigationSettings.AddNewPagesToNavigation = false;
    webNavigationSettings.CreateFriendlyUrlsForNewPages = false;

    Managed navigation: Term set

    To connect to a term set in code you can do something like this.

    // Below code has to be used from within a method of course.
    var session = new TaxonomySession(web.Site);
    if (session.TermStores.Count != 0)
    {
        var termStore = session.TermStores["MyTermStoreName"]; // Standard name is "Managed Metadata Service"
        var group = this.GetTermGroupByName(termStore.Groups, "MyTermGroupName");
        var termSet = this.GetTermSetByName(group.TermSets, "MyTermSetName");
        webNavigationSettings.GlobalNavigation.TermStoreId = termStore.Id;
        webNavigationSettings.GlobalNavigation.TermSetId = termSet.Id;
    }
    
    // Support methods
    private Group GetTermGroupByName(GroupCollection groupCollection, string name)
    {
        foreach (var group in groupCollection)
        {
            if (group.Name == name)
            {
                return group;
            }
        }
        return null;
    }
    
    private TermSet GetTermSetByName(TermSetCollection setCollection, string name)
    {
        foreach (var set in setCollection
        {
            if (set.Name == name)
            {
                return set;
            }
        }
        return null;
    }

    Structural navigation: Sorting

    Use the PublishingWeb.Navigation to set the ordering of the Structural navigation.

    pubWeb.Navigation.OrderingMethod = OrderingMethod.Automatic;
    pubWeb.Navigation.AutomaticSortingMethod = AutomaticSortingMethod.Title;

    Update
    Finally, don’t forget to update your objects when you are done with them.

    webNavigationSettings.Update();
    pubWeb.Update();
    web.Update();

    These examples are not a complete overview of what you can do with the navigation in SP2013, but the objects I have shown contain most of the properties needed to set the navigation. You just have to look at the object model in Visual Studio to find other options, and most are pretty self-explanatory.

  • 相关阅读:
    LOJ DFS序
    牛客练习赛31 D神器大师泰兹瑞与威穆
    Codeforces Round #487 (Div. 2) C
    Manthan, Codefest 18 (rated, Div. 1 + Div. 2) C D
    [Windows Server 2003] 还原SQL Server数据库
    [Windows Server 2008] 查看PHP详细错误信息
    [Windows Server 2008] 安装网站伪静态
    网站Gzip压缩
    [Windows Server 2008] SQL Server 2008 数据库还原方法
    [Windows Server 2008] 安装Apache+PHP+MySQL
  • 原文地址:https://www.cnblogs.com/frankzye/p/3109801.html
Copyright © 2011-2022 走看看