class OnenoteResource
Represents an image or other file resource on a OneNote page, providing access to the resource's binary content and metadata.
/tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/onenote/resources/resource.py
8 - 23
moderate
Purpose
This class models a OneNote resource (such as images, files, or other binary content) attached to a OneNote page. It extends OnenoteEntityBaseModel to provide specialized functionality for retrieving the binary content of resources and accessing their download URLs. The class is part of the Office 365 OneNote API integration and handles the interaction with the underlying API through a context-based query system.
Source Code
class OnenoteResource(OnenoteEntityBaseModel):
"""An image or other file resource on a OneNote page."""
def get_content(self):
# type: () -> ClientResult[AnyStr]
"""Retrieve the binary data of a file or image resource object."""
return_type = ClientResult(self.context)
qry = FunctionQuery(self, "content", None, return_type)
self.context.add_query(qry)
return return_type
@property
def content_url(self):
# type: () -> Optional[str]
"""The URL for downloading the content"""
return self.properties.get("contentUrl", None)
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
bases |
OnenoteEntityBaseModel | - |
Parameter Details
context: Inherited from OnenoteEntityBaseModel. The execution context that manages API communication and query execution for this resource entity.
properties: Inherited from OnenoteEntityBaseModel. A dictionary containing the resource's properties including contentUrl and other metadata returned from the OneNote API.
Return Value
Instantiation returns an OnenoteResource object that represents a specific resource on a OneNote page. The get_content() method returns a ClientResult[AnyStr] object that will contain the binary data of the resource once the query is executed. The content_url property returns an Optional[str] containing the URL for downloading the content, or None if not available.
Class Interface
Methods
get_content() -> ClientResult[AnyStr]
Purpose: Retrieves the binary data of the file or image resource object by creating and queuing a function query
Returns: A ClientResult object that will contain the binary content (AnyStr) of the resource after the query is executed via context.execute_query()
content_url -> Optional[str]
property
Purpose: Provides read-only access to the URL for downloading the resource content
Returns: A string containing the download URL for the resource content, or None if the URL is not available in the properties
Attributes
| Name | Type | Description | Scope |
|---|---|---|---|
context |
ClientContext | Inherited from base class. The execution context that manages API communication and query execution | instance |
properties |
dict | Inherited from base class. Dictionary containing the resource's properties including contentUrl and other metadata | instance |
Dependencies
typingoffice365.onenote.entity_base_modeloffice365.runtime.client_resultoffice365.runtime.queries.function
Required Imports
from typing import AnyStr, Optional
from office365.onenote.entity_base_model import OnenoteEntityBaseModel
from office365.runtime.client_result import ClientResult
from office365.runtime.queries.function import FunctionQuery
Usage Example
# Assuming you have a OneNote page object with resources
from office365.onenote.resources.onenote_resource import OnenoteResource
# Get a resource from a OneNote page (typically obtained from page.resources)
resource = page.resources[0] # OnenoteResource instance
# Get the download URL for the resource
download_url = resource.content_url
print(f"Resource URL: {download_url}")
# Retrieve the binary content of the resource
content_result = resource.get_content()
context.execute_query() # Execute the query to fetch the content
# Access the binary data
binary_data = content_result.value
with open('downloaded_resource.bin', 'wb') as f:
f.write(binary_data)
Best Practices
- Always call context.execute_query() after calling get_content() to actually fetch the binary data from the API
- Check if content_url is not None before attempting to use it for downloads
- The get_content() method returns a ClientResult object that acts as a promise - the actual value is populated after query execution
- This class is typically not instantiated directly but obtained through navigation properties from OneNote page objects
- Handle binary data appropriately based on the resource type (image, file, etc.)
- Be aware that retrieving content may involve network calls and should be done efficiently to avoid unnecessary API requests
- The context object must be properly authenticated and configured before using any methods
Similar Components
AI-powered semantic similarity - components with related functionality:
-
class Onenote 69.2% similar
-
class OnenoteEntityBaseModel 65.5% similar
-
class OnenoteEntityHierarchyModel 62.9% similar
-
class Notebook 62.0% similar
-
class OnenoteEntitySchemaObjectModel 58.9% similar