🔍 Code Extractor

class UserCustomActionCollection

Maturity: 36

A collection class that manages UserCustomAction entities in SharePoint, providing methods to interact with and manipulate custom actions as a group.

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

Purpose

UserCustomActionCollection serves as a container for managing multiple UserCustomAction objects in SharePoint. It extends EntityCollection to provide collection-specific operations like clearing all custom actions. This class is used when working with SharePoint custom actions at the site or web level, allowing bulk operations and enumeration of custom actions. It follows the SharePoint REST API pattern for entity collections.

Source Code

class UserCustomActionCollection(EntityCollection[UserCustomAction]):
    def __init__(self, context, resource_path=None):
        """Specifies a collection of custom actions."""
        super(UserCustomActionCollection, self).__init__(
            context, UserCustomAction, resource_path
        )

    def clear(self):
        """
        Deletes all custom actions in the collection.
        Exceptions:
        - 2130575305 Microsoft.SharePoint.SPException Custom action was modified on the server  in a way that
             prevents changes from being committed, as determined by the protocol server.
        """
        qry = ServiceOperationQuery(self, "Clear")
        self.context.add_query(qry)
        return self

Parameters

Name Type Default Kind
bases EntityCollection[UserCustomAction] -

Parameter Details

context: The client context object that manages the connection to SharePoint and handles request/response operations. This is required for all SharePoint API operations and maintains authentication state.

resource_path: Optional parameter specifying the URL path to the collection resource in SharePoint. If None, the path will be determined by the parent context. This allows targeting specific collection endpoints in the SharePoint REST API.

Return Value

Instantiation returns a UserCustomActionCollection object that can be used to manage custom actions. The clear() method returns self to enable method chaining. The collection inherits iteration and indexing capabilities from EntityCollection, allowing access to individual UserCustomAction items.

Class Interface

Methods

__init__(self, context, resource_path=None)

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

Parameters:

  • context: The client context object for SharePoint operations
  • resource_path: Optional URL path to the collection resource, defaults to None

Returns: None (constructor)

clear(self) -> UserCustomActionCollection

Purpose: Deletes all custom actions in the collection by invoking the Clear service operation on the SharePoint server

Returns: Returns self to enable method chaining. The actual deletion occurs when execute_query() is called on the context.

Attributes

Name Type Description Scope
context ClientContext The SharePoint client context inherited from EntityCollection, used for executing queries and managing server communication instance
resource_path str or None The URL path to this collection resource in the SharePoint REST API, inherited from EntityCollection instance

Dependencies

  • office365.runtime.queries.service_operation
  • office365.sharepoint.entity_collection
  • office365.sharepoint.usercustomactions.action

Required Imports

from office365.runtime.queries.service_operation import ServiceOperationQuery
from office365.sharepoint.entity_collection import EntityCollection
from office365.sharepoint.usercustomactions.action import UserCustomAction

Usage Example

from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.usercustomactions.collection import UserCustomActionCollection

# Authenticate and create context
ctx = ClientContext('https://contoso.sharepoint.com/sites/mysite').with_credentials(user_credentials)

# Get the custom actions collection
custom_actions = ctx.web.user_custom_actions
ctx.load(custom_actions)
ctx.execute_query()

# Iterate through custom actions
for action in custom_actions:
    print(f"Action: {action.title}, Location: {action.location}")

# Clear all custom actions
custom_actions.clear()
ctx.execute_query()

# The collection is now empty
print(f"Remaining actions: {len(custom_actions)}")

Best Practices

  • Always call ctx.execute_query() after operations like clear() to commit changes to SharePoint server
  • Load the collection with ctx.load() before accessing items to ensure data is retrieved from server
  • Handle the SPException (error code 2130575305) when clearing, as it indicates concurrent modifications on the server
  • Use method chaining with clear() since it returns self, allowing fluent API patterns
  • Be cautious with clear() as it permanently deletes all custom actions in the collection without confirmation
  • Ensure proper permissions before attempting to modify custom actions (requires elevated privileges)
  • Consider backing up custom actions before clearing, as the operation is irreversible

Similar Components

AI-powered semantic similarity - components with related functionality:

  • class UserCustomAction_v1 79.7% similar

    Represents a SharePoint custom action entity with properties for rights, description, and title resources.

    From: /tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/sharepoint/customactions/action.py
  • class UserCustomAction 79.7% similar

    A class representing a custom action in SharePoint that can be associated with lists, websites, or subsites, providing access to custom action properties like scripts and URLs.

    From: /tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/sharepoint/usercustomactions/action.py
  • class CustomActionElementCollection 78.7% similar

    A collection class that manages a group of CustomActionElement objects, inheriting from ClientValue to provide SharePoint client-side value functionality.

    From: /tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/sharepoint/customactions/element_collection.py
  • class UserActivityCollection 69.3% similar

    A collection class for managing UserActivity entities, providing access to user activities including recently used items from the user's drive and shared drives.

    From: /tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/directory/users/activities/collection.py
  • class ExternalUserCollection 68.1% similar

    A collection class for managing external users in SharePoint, providing methods to retrieve and interact with external user entities.

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