class Entity
Base class for SharePoint entities that provides common operations like create, read, update, and delete (CRUD) functionality for SharePoint objects.
/tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/sharepoint/entity.py
11 - 48
moderate
Purpose
Entity serves as the base class for all SharePoint-specific objects in the Office365 Python SDK. It extends ClientObject to provide SharePoint-specific functionality including credential management, entity deletion, updates, and metadata handling. This class is designed to be inherited by concrete SharePoint entity types (like List, File, Folder, etc.) and provides the foundational methods for interacting with SharePoint REST API endpoints.
Source Code
class Entity(ClientObject):
"""SharePoint specific entity"""
def with_credentials(self, credentials):
"""
:type self: T
:type credentials: UserCredential or ClientCredential
"""
self.context.with_credentials(credentials)
return self
def delete_object(self):
"""The recommended way to delete a SharePoint entity"""
qry = DeleteEntityQuery(self)
self.context.add_query(qry)
self.remove_from_parent_collection()
return self
def update(self, *args):
"""The recommended way to update a SharePoint entity"""
qry = UpdateEntityQuery(self)
self.context.add_query(qry)
return self
@property
def context(self):
# type: () -> ClientContext
return self._context
@property
def entity_type_name(self):
if self._entity_type_name is None:
self._entity_type_name = ".".join(["SP", type(self).__name__])
return self._entity_type_name
@property
def property_ref_name(self):
return "Id"
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
bases |
ClientObject | - |
Parameter Details
__init__: Inherits constructor from ClientObject base class. The constructor typically accepts a ClientContext instance that manages the connection to SharePoint and tracks entity state.
Return Value
Instantiation returns an Entity object representing a SharePoint entity. Methods like with_credentials(), delete_object(), and update() return self to enable method chaining. The context property returns a ClientContext instance, entity_type_name returns a string with the SharePoint type name (e.g., 'SP.List'), and property_ref_name returns the string 'Id'.
Class Interface
Methods
with_credentials(self, credentials) -> Entity
Purpose: Sets authentication credentials for the entity's context to enable authenticated operations
Parameters:
credentials: UserCredential or ClientCredential object containing authentication information for SharePoint access
Returns: Returns self to enable method chaining
delete_object(self) -> Entity
Purpose: Deletes the SharePoint entity by creating a delete query and removing it from parent collection
Returns: Returns self to enable method chaining. The actual deletion occurs when context.execute_query() is called
update(self, *args) -> Entity
Purpose: Updates the SharePoint entity by creating an update query to persist changes made to entity properties
Parameters:
args: Variable arguments (currently unused but available for extension)
Returns: Returns self to enable method chaining. The actual update occurs when context.execute_query() is called
context(self) -> ClientContext
property
Purpose: Provides access to the ClientContext instance that manages this entity's connection to SharePoint
Returns: ClientContext instance associated with this entity
entity_type_name(self) -> str
property
Purpose: Returns the SharePoint entity type name in the format 'SP.ClassName', used for REST API operations
Returns: String representing the SharePoint type name (e.g., 'SP.List', 'SP.File'). Lazily initialized and cached
property_ref_name(self) -> str
property
Purpose: Returns the property name used to reference this entity, typically for building REST API URLs
Returns: String 'Id' which is the default reference property for SharePoint entities
Attributes
| Name | Type | Description | Scope |
|---|---|---|---|
_context |
ClientContext | Private attribute storing the ClientContext instance that manages the SharePoint connection and query execution | instance |
_entity_type_name |
str or None | Private cached attribute storing the SharePoint entity type name. Initialized to None and populated on first access | instance |
Dependencies
office365-runtimetyping
Required Imports
from office365.runtime.client_object import ClientObject
from office365.runtime.queries.delete_entity import DeleteEntityQuery
from office365.runtime.queries.update_entity import UpdateEntityQuery
from office365.sharepoint.client_context import ClientContext
Usage Example
from office365.sharepoint.client_context import ClientContext
from office365.runtime.auth.user_credential import UserCredential
from office365.sharepoint.lists.list import List
# Create context and authenticate
ctx = ClientContext('https://tenant.sharepoint.com/sites/site')
credentials = UserCredential('user@tenant.com', 'password')
# Get a list entity
list_entity = ctx.web.lists.get_by_title('Documents')
# Set credentials and update
list_entity.with_credentials(credentials).update()
ctx.execute_query()
# Delete the entity
list_entity.delete_object()
ctx.execute_query()
# Access entity metadata
print(list_entity.entity_type_name) # 'SP.List'
print(list_entity.property_ref_name) # 'Id'
Best Practices
- Always call ctx.execute_query() after delete_object() or update() to commit changes to SharePoint
- Use method chaining with with_credentials() for cleaner code
- Ensure a valid ClientContext is associated before performing operations
- The Entity class is meant to be inherited, not instantiated directly
- Call update() after modifying entity properties to persist changes
- Use delete_object() instead of manually removing entities to ensure proper cleanup
- The entity_type_name property is lazily initialized and cached
- Credentials set via with_credentials() affect the entire context, not just the entity
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
class Utility_v1 71.7% similar
-
class BaseCustomProperty 70.8% similar
-
class Authentication 68.0% similar
-
class EntityCollection 67.7% similar
-
class SecurableObjectExtensions 67.4% similar