0

When Going to Site settings>Navigation for a subsite i want to modify The Structural Navigation:Sorting to be the first value (Sort automatically)

I am trying to do this with my code:

ClientContext clientContext;
var navigation = new ClientPortalNavigation(clientContext.Web);
navigation.CurrentIncludePages = true;
navigation.GlobalIncludePages = false;
navigation.OrderingMethod = OrderingMethod.Automatic; // Settings
navigation.SaveChanges();

I have my code in a button so when pressing the button I want to set it to be automatic

I have also create a class by the name ClientPortalNavigation.cs My cs file looks like this,

public class ClientPortalNavigation
{
public ClientPortalNavigation(Web web)
{
    _web = web;
}

#region CRUD operations

private void EnsureLoaded()
{
    if (!_web.IsObjectPropertyInstantiated("AllProperties"))
    {
        Context.Load(_web, w => w.AllProperties);
        Context.ExecuteQuery();
    }
}


public void SaveChanges()
{
    _web.Update();
    Context.ExecuteQuery();
}
public bool GlobalIncludePages
{
    get
    {
        return (GlobalIncludeTypes & NodeTypes.Page) == NodeTypes.Page;
    }
    set
    {
        GlobalIncludeTypes = !value ? GlobalIncludeTypes & ~NodeTypes.Page : GlobalIncludeTypes | NodeTypes.Page;
    }
}


/// <summary>
/// Controls whether publishing pages in this site will be automatically included in current navigation.
/// </summary>
public bool CurrentIncludePages
{
    get
    {
        return (CurrentIncludeTypes & NodeTypes.Page) == NodeTypes.Page;
    }
    set
    {
        CurrentIncludeTypes = !value ? CurrentIncludeTypes & ~NodeTypes.Page : CurrentIncludeTypes | NodeTypes.Page;
    }
}
/// <summary>
/// Options that specify how navigation items are ordered.
/// </summary>
public enum OrderingMethod
{
Automatic,
ManualWithAutomaticPageSorting,
Manual,
}
/// <summary>
/// Controls the ordering of navigation items owned by this site.
/// </summary>
public OrderingMethod OrderingMethod
{
    get
    {
        return (OrderingMethod)GetProperty("__NavigationOrderingMethod", 2);
    }
    set
    {
        SetProperty("__NavigationOrderingMethod", (int)value);
    }
}
}
1

Most probably you are using the ClientPortalNavigation.cs class for managing navigation settings using SharePoint CSOM.

Disclaimer: i'm the author of this class

The following examples demonstrate how to set Structural Navigation options.

Example 1

using (var ctx = new ClientContext(webUrl))
{
     var navigation = new ClientPortalNavigation(ctx.Web);
     navigation.OrderingMethod = OrderingMethod.Automatic;
     navigation.SaveChanges();           
}

Result

enter image description here

Example 2

using (var ctx = new ClientContext(webUrl))
{
     var navigation = new ClientPortalNavigation(ctx.Web);
     navigation.OrderingMethod = OrderingMethod.Automatic;
     navigation.AutomaticSortingMethod = AutomaticSortingMethod.LastModifiedDate;
     navigation.SaveChanges();           
}

Result

enter image description here

Example 3

using (var ctx = new ClientContext(webUrl))
{
     var navigation = new ClientPortalNavigation(ctx.Web);
     navigation.OrderingMethod = OrderingMethod.Automatic;
     navigation.AutomaticSortingMethod = AutomaticSortingMethod.LastModifiedDate;
     navigation.SortAscending = false;
     navigation.SaveChanges();           
}

Result

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.