class Comment
Represents a SharePoint comment entity with social interaction capabilities including liking/unliking and tracking users who liked the comment.
/tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/sharepoint/comments/comment.py
8 - 48
moderate
Purpose
The Comment class extends the Entity base class to provide functionality for managing SharePoint comments. It enables users to like or unlike comments, retrieve the list of users who liked a comment, and access comment-specific properties. This class is part of the SharePoint API integration for handling comment interactions in SharePoint sites, lists, or documents.
Source Code
class Comment(Entity):
def like(self):
"""
The Like method makes the current user a liker of the comment.
"""
qry = ServiceOperationQuery(self, "Like")
self.context.add_query(qry)
return self
def unlike(self):
"""
The Unlike method removes the current user from the list of likers for the comment.
"""
qry = ServiceOperationQuery(self, "Unlike")
self.context.add_query(qry)
return self
@property
def liked_by(self):
"""
List of like entries corresponding to individual likes. MUST NOT contain more than one entry
for the same user in the set.
"""
return self.properties.get(
"likedBy",
EntityCollection(
self.context, UserEntity, ResourcePath("likedBy", self.resource_path)
),
)
@property
def entity_type_name(self):
return "Microsoft.SharePoint.Comments.comment"
def get_property(self, name, default_value=None):
if default_value is None:
property_mapping = {
"likedBy": self.liked_by,
}
default_value = property_mapping.get(name, None)
return super(Comment, self).get_property(name, default_value)
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
bases |
Entity | - |
Parameter Details
__init__: Inherits constructor from Entity base class. Typically requires a context object (ClientContext) and optionally a resource path to identify the specific comment resource in SharePoint.
Return Value
Instantiation returns a Comment object representing a SharePoint comment entity. The like() and unlike() methods return self for method chaining. The liked_by property returns an EntityCollection of UserEntity objects representing users who liked the comment. The get_property() method returns the requested property value or a default value if not found.
Class Interface
Methods
like() -> Comment
Purpose: Makes the current authenticated user a liker of the comment
Returns: Returns self (the Comment instance) for method chaining
unlike() -> Comment
Purpose: Removes the current authenticated user from the list of likers for the comment
Returns: Returns self (the Comment instance) for method chaining
@property liked_by -> EntityCollection
property
Purpose: Retrieves the collection of users who have liked this comment
Returns: EntityCollection of UserEntity objects representing users who liked the comment. Must not contain duplicate entries for the same user.
@property entity_type_name -> str
property
Purpose: Returns the SharePoint entity type identifier for comment entities
Returns: String 'Microsoft.SharePoint.Comments.comment' identifying the entity type
get_property(name: str, default_value=None) -> Any
Purpose: Retrieves a property value by name with optional default value fallback, with special handling for mapped properties like 'likedBy'
Parameters:
name: The name of the property to retrieve (e.g., 'likedBy')default_value: Optional default value to return if property is not found. If None, uses internal property mapping.
Returns: The property value if found, otherwise the default_value or mapped property from internal mapping
Attributes
| Name | Type | Description | Scope |
|---|---|---|---|
context |
ClientContext | Inherited from Entity. The SharePoint client context used for executing queries and managing the connection to SharePoint | instance |
properties |
dict | Inherited from Entity. Dictionary storing the comment's properties and metadata retrieved from SharePoint | instance |
resource_path |
ResourcePath | Inherited from Entity. The resource path identifying this comment's location in the SharePoint API hierarchy | instance |
Dependencies
office365.runtime.paths.resource_pathoffice365.runtime.queries.service_operationoffice365.sharepoint.entityoffice365.sharepoint.entity_collectionoffice365.sharepoint.likes.user_entity
Required Imports
from office365.runtime.paths.resource_path import ResourcePath
from office365.runtime.queries.service_operation import ServiceOperationQuery
from office365.sharepoint.entity import Entity
from office365.sharepoint.entity_collection import EntityCollection
from office365.sharepoint.likes.user_entity import UserEntity
Usage Example
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.comments.comment import Comment
# Authenticate and create context
ctx = ClientContext(site_url).with_credentials(credentials)
# Get a comment (typically retrieved from a list or document)
comment = ctx.web.lists.get_by_title('Documents').items.get_by_id(1).comments.get_by_id(5)
ctx.load(comment)
ctx.execute_query()
# Like the comment
comment.like()
ctx.execute_query()
# Get users who liked the comment
liked_by_users = comment.liked_by
ctx.load(liked_by_users)
ctx.execute_query()
for user in liked_by_users:
print(user.properties['loginName'])
# Unlike the comment
comment.unlike()
ctx.execute_query()
Best Practices
- Always call ctx.execute_query() after like() or unlike() operations to commit changes to SharePoint
- Use method chaining with like() and unlike() since they return self
- Load the liked_by collection explicitly using ctx.load() before accessing user details
- Ensure the current user has appropriate permissions before attempting like/unlike operations
- The liked_by collection must not contain duplicate entries for the same user
- Use get_property() method to safely access properties with fallback defaults
- The entity_type_name property identifies this as a SharePoint comment entity type
Similar Components
AI-powered semantic similarity - components with related functionality:
-
class CommentCollection 76.3% similar
-
class CommentInformation 72.1% similar
-
class LikedByInformation 71.8% similar
-
class Identity_v1 68.7% similar
-
class UserEntity 66.1% similar