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.

  • 相关阅读:
    python for test
    python链接mysql pymysql
    MongoDB数据表添加字段
    NodeVisitor
    无法解决的错误
    一个点绕着另一个点旋转一定角度后的坐标
    2.0版本里程碑,研发日志
    osg Node getParentalNodePaths()报错
    TeslaManage 2.0编译日志
    机械臂模拟2.0
  • 原文地址:https://www.cnblogs.com/frankzye/p/3109801.html
Copyright © 2011-2022 走看看