🔍 Code Extractor

class ClientWebPartCollection

Maturity: 48

A collection class that manages ClientWebPart objects representing all client-side web parts installed in a SharePoint Web.

File:
/tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/sharepoint/webparts/client/collection.py
Lines:
6 - 18
Complexity:
moderate

Purpose

ClientWebPartCollection serves as a container and manager for ClientWebPart instances within a SharePoint Web context. It extends EntityCollection to provide collection-specific operations for web parts, including retrieval by ID. This class is used to interact with the collection of client-side web parts available in a SharePoint site, enabling enumeration and access to individual web part configurations.

Source Code

class ClientWebPartCollection(EntityCollection[ClientWebPart]):
    """Collection of ClientWebPart representations. It includes all ClientWebParts installed in the SP.Web."""

    def __init__(self, context, resource_path=None):
        super(ClientWebPartCollection, self).__init__(
            context, ClientWebPart, resource_path
        )

    def get_by_id(self, wp_id):
        """Gets the Client web part with the specified ID."""
        return ClientWebPart(
            self.context, ServiceOperationPath("getById", [wp_id], self.resource_path)
        )

Parameters

Name Type Default Kind
bases EntityCollection[ClientWebPart] -

Parameter Details

context: The client context object that provides the connection and authentication information for SharePoint operations. This is required for all SharePoint API interactions and maintains the session state.

resource_path: Optional ServiceOperationPath or ResourcePath object that specifies the URL path to the web part collection resource in SharePoint. If None, a default path will be used based on the parent context.

Return Value

Instantiation returns a ClientWebPartCollection object that can be used to access and manage ClientWebPart instances. The get_by_id method returns a single ClientWebPart object corresponding to the specified web part ID. As an EntityCollection subclass, iteration over the collection yields individual ClientWebPart objects.

Class Interface

Methods

__init__(self, context, resource_path=None)

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

Parameters:

  • context: The client context object for SharePoint operations
  • resource_path: Optional path to the web part collection resource (defaults to None)

Returns: None (constructor)

get_by_id(self, wp_id) -> ClientWebPart

Purpose: Retrieves a specific ClientWebPart from the collection using its unique identifier

Parameters:

  • wp_id: The unique identifier (GUID string) of the web part to retrieve

Returns: A ClientWebPart object representing the web part with the specified ID. The object needs to be loaded via ctx.load() and ctx.execute_query() to populate its properties.

Attributes

Name Type Description Scope
context ClientContext The SharePoint client context inherited from EntityCollection, used for all API operations instance
resource_path ResourcePath or ServiceOperationPath The URL path to the web part collection resource in SharePoint, inherited from EntityCollection instance

Dependencies

  • office365

Required Imports

from office365.sharepoint.webparts.client.webpart_collection import ClientWebPartCollection
from office365.sharepoint.webparts.client.webpart import ClientWebPart
from office365.runtime.paths.service_operation import ServiceOperationPath
from office365.sharepoint.entity_collection import EntityCollection

Usage Example

from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.webparts.client.webpart_collection import ClientWebPartCollection

# Authenticate and create context
ctx = ClientContext('https://yourtenant.sharepoint.com/sites/yoursite').with_credentials(
    UserCredential('username', 'password')
)

# Get the web parts collection
web = ctx.web
web_parts = web.get_client_web_parts()
ctx.load(web_parts)
ctx.execute_query()

# Iterate through all web parts
for wp in web_parts:
    print(f'Web Part: {wp.properties}')

# Get a specific web part by ID
wp_id = 'a1b2c3d4-e5f6-7890-abcd-ef1234567890'
specific_wp = web_parts.get_by_id(wp_id)
ctx.load(specific_wp)
ctx.execute_query()
print(f'Retrieved web part: {specific_wp.properties}')

Best Practices

  • Always call ctx.load() and ctx.execute_query() after retrieving the collection or individual web parts to populate their properties from SharePoint
  • Use get_by_id() when you know the specific web part ID to avoid loading the entire collection
  • Ensure the client context has appropriate permissions to access web part information in the SharePoint site
  • The collection inherits from EntityCollection, so standard collection operations like iteration, filtering, and indexing are available
  • Web part IDs are typically GUIDs in string format; ensure proper formatting when using get_by_id()
  • Consider caching the collection if you need to access multiple web parts to minimize round trips to SharePoint
  • Handle exceptions for cases where a web part ID doesn't exist or the user lacks permissions

Similar Components

AI-powered semantic similarity - components with related functionality:

  • class WebPartDefinitionCollection 80.3% similar

    A collection class that manages and provides access to WebPartDefinition objects in SharePoint, extending EntityCollection with specialized methods for Web Part definitions.

    From: /tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/sharepoint/webparts/definition_collection.py
  • class ClientWebPart 78.6% similar

    Represents a SharePoint ClientWebPart entity that provides metadata and rendering capabilities for web parts in SharePoint client-side applications.

    From: /tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/sharepoint/webparts/client/webpart.py
  • class WebCollection 75.4% 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 WebPart 72.7% similar

    A class representing a SharePoint Web Part, which is a reusable component that contains or generates web-based content (XML, HTML, scripting) and displays it in a cohesive unit on a webpage.

    From: /tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/sharepoint/webparts/webpart.py
  • class EntityCollection 71.2% similar

    EntityCollection is a generic collection class for managing SharePoint entity sets, extending ClientObjectCollection with SharePoint-specific functionality.

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