🔍 Code Extractor

class OnenotePageCollection

Maturity: 48

A collection class for managing OneNote pages within a OneNote notebook, providing methods to create and manage multiple OnenotePage instances.

File:
/tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/onenote/pages/collection.py
Lines:
8 - 23
Complexity:
moderate

Purpose

This class serves as a container and manager for OneNote pages, inheriting from EntityCollection to provide collection-based operations. It enables creating new OneNote pages with presentation files and optional attachments, managing them as a collection within the Microsoft Graph API context. The class handles the queuing of page creation operations through the context's query system.

Source Code

class OnenotePageCollection(EntityCollection[OnenotePage]):
    """A collection of pages in a OneNote notebook"""

    def __init__(self, context, resource_path=None):
        super(OnenotePageCollection, self).__init__(context, OnenotePage, resource_path)

    def add(self, presentation_file, attachment_files=None):
        # type: (IO, dict) -> OnenotePage
        """
        Create a new OneNote page.
        :param typing.IO presentation_file: Presentation file
        :param dict or None attachment_files: Attachment files
        """
        qry = OneNotePageCreateQuery(self, presentation_file, attachment_files)
        self.context.add_query(qry)
        return qry.return_type

Parameters

Name Type Default Kind
bases EntityCollection[OnenotePage] -

Parameter Details

context: The client context object that manages API communication and authentication with Microsoft Graph API. This context is used to execute queries and maintain the connection state.

resource_path: Optional string representing the API endpoint path for this collection. If None, a default path will be used. This path is used to construct the full URL for API requests targeting this specific collection of pages.

Return Value

Instantiation returns an OnenotePageCollection object that can manage OneNote pages. The add() method returns a OnenotePage object representing the newly created page, which will be populated with data once the query is executed through the context.

Class Interface

Methods

__init__(self, context, resource_path=None)

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

Parameters:

  • context: The client context object for API communication
  • resource_path: Optional string path to the API endpoint for this collection

Returns: None (constructor)

add(self, presentation_file: IO, attachment_files: dict = None) -> OnenotePage

Purpose: Creates a new OneNote page with the provided presentation file and optional attachments, queuing the creation operation in the context

Parameters:

  • presentation_file: A file-like object (IO) containing the HTML content for the OneNote page
  • attachment_files: Optional dictionary mapping attachment names (strings) to file-like objects (IO) to be included with the page

Returns: A OnenotePage object representing the page to be created. The object will be populated with server data after context.execute_query() is called

Attributes

Name Type Description Scope
context ClientContext The client context object inherited from EntityCollection, used for executing API queries and managing authentication instance
resource_path str or None The API endpoint path for this collection, inherited from EntityCollection instance

Dependencies

  • office365
  • typing

Required Imports

from office365.onenote.pages.onenote_page_collection import OnenotePageCollection
from office365.onenote.pages.page import OnenotePage
from typing import IO

Usage Example

# Assuming you have a valid context object
from office365.onenote.pages.onenote_page_collection import OnenotePageCollection

# Initialize the collection with a context
page_collection = OnenotePageCollection(context, resource_path='/me/onenote/pages')

# Create a new page with a presentation file
with open('page_content.html', 'rb') as presentation_file:
    # Optional: add attachments
    attachments = {
        'image1': open('image.png', 'rb'),
        'document1': open('doc.pdf', 'rb')
    }
    
    # Add a new page to the collection
    new_page = page_collection.add(presentation_file, attachments)
    
    # Execute the query through context
    context.execute_query()
    
    # Access the created page properties
    print(f'Created page ID: {new_page.id}')

Best Practices

  • Always ensure the context object is properly authenticated before creating a collection instance
  • Call context.execute_query() after adding pages to actually execute the API request
  • The add() method returns a OnenotePage object immediately, but it won't have server-generated properties (like ID) until execute_query() is called
  • Close file handles properly after passing them to the add() method, or use context managers (with statements)
  • Handle API exceptions that may occur during execute_query() execution
  • The collection inherits from EntityCollection[OnenotePage], so it supports standard collection operations like iteration and indexing
  • Attachment files should be provided as a dictionary where keys are attachment names and values are file-like objects
  • The presentation_file parameter should contain valid HTML content for the OneNote page

Similar Components

AI-powered semantic similarity - components with related functionality:

  • class NotebookCollection 84.0% similar

    A collection class for managing OneNote notebooks, providing methods to create, retrieve, and query notebooks within Microsoft 365, SharePoint, or group contexts.

    From: /tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/onenote/notebooks/collection.py
  • class Onenote 75.8% similar

    The Onenote class serves as the entry point for accessing Microsoft OneNote resources through the Microsoft Graph API, providing access to notebooks, pages, sections, and other OneNote entities.

    From: /tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/onenote/onenote.py
  • class Notebook 75.0% similar

    Represents a OneNote notebook entity with access to its sections and section groups, providing a hierarchical structure for organizing OneNote content.

    From: /tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/onenote/notebooks/notebook.py
  • class OnenotePage 73.5% similar

    A page in a OneNote notebook.

    From: /tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/onenote/pages/page.py
  • class OneNotePageCreateQuery 70.7% similar

    A specialized query class for creating OneNote pages with multipart form-data requests, handling presentation files and optional attachments.

    From: /tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/onenote/internal/multipart_page_query.py
← Back to Browse