🔍 Code Extractor

class NavigationNodeCollection

Maturity: 38

A collection class for managing SharePoint navigation nodes, providing methods to add, retrieve, and manipulate navigation nodes within a SharePoint site's navigation structure.

File:
/tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/sharepoint/navigation/node_collection.py
Lines:
15 - 68
Complexity:
moderate

Purpose

NavigationNodeCollection manages a collection of NavigationNode objects in SharePoint. It extends EntityCollection to provide specialized functionality for navigation node operations including creating new nodes, retrieving nodes by index or ID, and reordering nodes. This class is used to interact with SharePoint's navigation menus (quick launch, top navigation, etc.) and provides a programmatic interface for navigation structure management.

Source Code

class NavigationNodeCollection(EntityCollection[NavigationNode]):
    def __init__(self, context, resource_path=None):
        super(NavigationNodeCollection, self).__init__(
            context, NavigationNode, resource_path
        )

    def add(self, create_node_info):
        # type: (NavigationNodeCreationInformation) -> NavigationNode
        """
        Creates a navigation node object and adds it to the collection.
        """
        return_type = NavigationNode(self.context)
        return_type.title = create_node_info.Title
        return_type.url = create_node_info.Url
        self.add_child(return_type)
        qry = CreateEntityQuery(self, return_type, return_type)
        self.context.add_query(qry)
        return return_type

    def move_after(self, node_id, previous_node_id):
        """
        Moves a navigation node after a specified navigation node in the navigation node collection.

        :param int node_id: Identifier of the navigation node that is moved.
        :param int previous_node_id: Identifier of the navigation node after which the node identified by nodeId moves to
        """
        params = {"nodeId": node_id, "previousNodeId": previous_node_id}
        qry = ServiceOperationQuery(self, "GetByIndex", params)
        self.context.add_query(qry)
        return self

    def get_by_index(self, index):
        """
        Returns the navigation node at the specified index.

        :param int index: The index of the navigation node to be returned.
        """
        return_type = NavigationNode(self.context)
        self.add_child(return_type)
        qry = ServiceOperationQuery(
            self, "GetByIndex", [index], None, None, return_type
        )
        self.context.add_query(qry)
        return return_type

    def get_by_id(self, node_id):
        """Returns the navigation node with the specified identifier.
        It MUST return NULL if no navigation node corresponds to the specified identifier.

        :param int node_id: Specifies the identifier of the navigation node.
        """
        return NavigationNode(
            self.context, ServiceOperationPath("GetById", [node_id], self.resource_path)
        )

Parameters

Name Type Default Kind
bases EntityCollection[NavigationNode] -

Parameter Details

context: The client context object that maintains the connection to SharePoint and manages query execution. This is required for all operations and is passed to child NavigationNode objects.

resource_path: Optional. The resource path in the SharePoint REST API that identifies this collection. If not provided, it will be determined based on the parent entity. Used for constructing API endpoints for operations.

Return Value

Instantiation returns a NavigationNodeCollection object that can be used to manage navigation nodes. Methods return: add() returns a new NavigationNode object; get_by_index() and get_by_id() return NavigationNode objects; move_after() returns self for method chaining.

Class Interface

Methods

__init__(self, context, resource_path=None)

Purpose: Initializes a new NavigationNodeCollection instance with the provided context and optional resource path

Parameters:

  • context: The client context object for SharePoint operations
  • resource_path: Optional resource path for the collection in the SharePoint REST API

Returns: None (constructor)

add(self, create_node_info: NavigationNodeCreationInformation) -> NavigationNode

Purpose: Creates a new navigation node with the specified information and adds it to the collection

Parameters:

  • create_node_info: NavigationNodeCreationInformation object containing Title and Url properties for the new node

Returns: A new NavigationNode object that has been added to the collection and queued for creation in SharePoint

move_after(self, node_id: int, previous_node_id: int) -> NavigationNodeCollection

Purpose: Moves a navigation node to appear after another specified node in the collection order

Parameters:

  • node_id: Integer identifier of the navigation node to be moved
  • previous_node_id: Integer identifier of the navigation node after which the target node should be positioned

Returns: Self (the NavigationNodeCollection instance) for method chaining

get_by_index(self, index: int) -> NavigationNode

Purpose: Retrieves the navigation node at the specified zero-based index position in the collection

Parameters:

  • index: Zero-based integer index of the navigation node to retrieve

Returns: A NavigationNode object at the specified index position

get_by_id(self, node_id: int) -> NavigationNode

Purpose: Retrieves the navigation node with the specified unique identifier, returns NULL if not found

Parameters:

  • node_id: Integer identifier of the navigation node to retrieve

Returns: A NavigationNode object with the specified ID, or NULL if no matching node exists

Attributes

Name Type Description Scope
context ClientContext The SharePoint client context used for executing queries and managing the connection (inherited from EntityCollection) instance
resource_path ResourcePath The resource path identifying this collection in the SharePoint REST API (inherited from EntityCollection) instance

Dependencies

  • office365

Required Imports

from office365.sharepoint.navigation.node_collection import NavigationNodeCollection
from office365.sharepoint.navigation.node_creation_information import NavigationNodeCreationInformation

Usage Example

from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.navigation.node_creation_information import NavigationNodeCreationInformation

# Initialize context with SharePoint site
ctx = ClientContext(site_url).with_credentials(credentials)

# Get the navigation node collection (e.g., quick launch)
nav_collection = ctx.web.navigation.quick_launch

# Add a new navigation node
node_info = NavigationNodeCreationInformation()
node_info.Title = 'New Link'
node_info.Url = 'https://example.com'
new_node = nav_collection.add(node_info)
ctx.execute_query()

# Get a node by index
node = nav_collection.get_by_index(0)
ctx.load(node)
ctx.execute_query()
print(node.title)

# Get a node by ID
node = nav_collection.get_by_id(1001)
ctx.load(node)
ctx.execute_query()

# Move a node after another node
nav_collection.move_after(node_id=1002, previous_node_id=1001)
ctx.execute_query()

Best Practices

  • Always call ctx.execute_query() after operations to commit changes to SharePoint
  • Use ctx.load() before accessing properties of retrieved NavigationNode objects
  • Create NavigationNodeCreationInformation objects with Title and Url before calling add()
  • The context object must be properly authenticated before using this collection
  • Node IDs are integers assigned by SharePoint and should be retrieved from existing nodes
  • move_after() requires both node IDs to exist in the collection
  • This class inherits from EntityCollection, so standard collection operations (iteration, filtering) are available
  • Operations are queued and executed when execute_query() is called on the context

Similar Components

AI-powered semantic similarity - components with related functionality:

  • class Navigation 79.7% similar

    Represents navigation operations at the SharePoint site collection level, providing access to Quick Launch and Top Navigation Bar node collections.

    From: /tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/sharepoint/navigation/navigation.py
  • class NavigationNode 70.2% similar

    Represents the URL to a specific navigation node and provides access to properties and methods for manipulating the ordering of the navigation node in a navigation node collection.

    From: /tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/sharepoint/navigation/node.py
  • class NavigationService 68.1% similar

    NavigationService is a REST-based service class for managing SharePoint navigation operations, including global navigation, menu states, and publishing navigation providers.

    From: /tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/sharepoint/navigation/navigation_service.py
  • class WebCollection 68.0% similar

    WebCollection is a specialized collection class for managing SharePoint Web objects, providing methods to add new webs and handle web-specific resource paths.

    From: /tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/sharepoint/webs/collection.py
  • class ConfiguredMetadataNavigationItemCollection 67.4% similar

    A collection class that manages a set of configured metadata navigation items, inheriting from ClientValue to provide client-side value representation.

    From: /tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/sharepoint/navigation/configured_metadata_items.py
← Back to Browse