class UserCustomAction
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.
/tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/sharepoint/usercustomactions/action.py
4 - 55
moderate
Purpose
This class models SharePoint custom actions, which are extensibility points that allow developers to inject custom ECMAScript code or link to external scripts that execute when certain events occur in SharePoint. It provides properties to access and modify the client-side component ID, script blocks, script sources, and URLs associated with the custom action. The class inherits from Entity, suggesting it's part of a larger SharePoint object model framework.
Source Code
class UserCustomAction(Entity):
"""Represents a custom action associated with a SharePoint list, Web site, or subsite."""
@property
def client_side_component_id(self):
"""
:rtype: str or None
"""
return self.properties.get("ClientSideComponentId", None)
@property
def script_block(self):
"""
Gets the value that specifies the ECMAScript to be executed when the custom action is performed.
:rtype: str or None
"""
return self.properties.get("ScriptBlock", None)
@script_block.setter
def script_block(self, value):
"""
Sets the value that specifies the ECMAScript to be executed when the custom action is performed.
:type value: str
"""
self.set_property("ScriptBlock", value)
@property
def script_src(self):
"""
Gets a value that specifies the URI of a file which contains the ECMAScript to execute on the page
:rtype: str or None
"""
return self.properties.get("ScriptSrc", None)
@script_src.setter
def script_src(self, value):
"""
Sets a value that specifies the URI of a file which contains the ECMAScript to execute on the page
:type value: str
"""
self.set_property("ScriptSrc", value)
@property
def url(self):
"""
Gets or sets the URL, URI, or ECMAScript (JScript, JavaScript) function associated with the action.
:rtype: str or None
"""
return self.properties.get("Url", None)
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
bases |
Entity | - |
Parameter Details
bases: Inherits from Entity class, which likely provides base functionality for SharePoint entities including property management and server communication capabilities. No explicit __init__ parameters are shown in the provided code, suggesting it uses the parent Entity class constructor.
Return Value
Instantiation returns a UserCustomAction object that represents a SharePoint custom action. Properties return either string values or None if the property is not set. The setters do not return values (implicit None) but modify the internal state of the object.
Class Interface
Methods
@property client_side_component_id() -> str | None
property
Purpose: Gets the client-side component identifier for the custom action
Returns: String containing the client-side component ID, or None if not set
@property script_block() -> str | None
property
Purpose: Gets the ECMAScript code to be executed when the custom action is performed
Returns: String containing the inline ECMAScript code, or None if not set
@script_block.setter script_block(value: str) -> None
property
Purpose: Sets the ECMAScript code to be executed when the custom action is performed
Parameters:
value: String containing the ECMAScript code to execute
Returns: None (setter method)
@property script_src() -> str | None
property
Purpose: Gets the URI of a file containing ECMAScript to execute on the page
Returns: String containing the URI/URL of the script file, or None if not set
@script_src.setter script_src(value: str) -> None
property
Purpose: Sets the URI of a file containing ECMAScript to execute on the page
Parameters:
value: String containing the URI/URL of the script file
Returns: None (setter method)
@property url() -> str | None
property
Purpose: Gets the URL, URI, or ECMAScript function associated with the custom action
Returns: String containing the URL, URI, or function name, or None if not set
Attributes
| Name | Type | Description | Scope |
|---|---|---|---|
properties |
dict | Inherited from Entity class, stores the property values for the custom action including ClientSideComponentId, ScriptBlock, ScriptSrc, and Url | instance |
Dependencies
office365.sharepoint.entity
Required Imports
from office365.sharepoint.entity import Entity
Usage Example
# Assuming you have a SharePoint context object
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.user_custom_actions.user_custom_action import UserCustomAction
# Get context (authentication details omitted for brevity)
ctx = ClientContext(site_url)
# Retrieve a custom action from a web
custom_action = ctx.web.user_custom_actions.get_by_id(action_id)
ctx.load(custom_action)
ctx.execute_query()
# Read properties
script_block = custom_action.script_block
script_src = custom_action.script_src
url = custom_action.url
component_id = custom_action.client_side_component_id
# Modify properties
custom_action.script_block = "alert('Hello from custom action');"
custom_action.script_src = "https://example.com/custom-script.js"
custom_action.update()
ctx.execute_query()
Best Practices
- Always load the custom action from SharePoint context before accessing properties to ensure data is populated
- Call update() and execute_query() after modifying properties to persist changes to SharePoint
- Check if property values are None before using them, as not all custom actions have all properties set
- Use script_src for external scripts rather than inline script_block for better maintainability
- Ensure proper SharePoint permissions are granted before attempting to modify custom actions
- The class follows a property-based pattern where getters return values from an internal properties dictionary
- Setters use set_property() method inherited from Entity to track changes for server updates
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
class UserCustomAction_v1 85.2% similar
-
class UserCustomActionCollection 79.7% similar
-
class CustomActionElement 76.8% similar
-
class CustomActionElementCollection 70.0% similar
-
class AnalyticsAction 63.1% similar