🔍 Code Extractor

class Comment

Maturity: 36

Represents a SharePoint comment entity with social interaction capabilities including liking/unliking and tracking users who liked the comment.

File:
/tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/sharepoint/comments/comment.py
Lines:
8 - 48
Complexity:
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_path
  • office365.runtime.queries.service_operation
  • office365.sharepoint.entity
  • office365.sharepoint.entity_collection
  • office365.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

    A collection class for managing SharePoint comments, providing operations to interact with multiple Comment entities as a group.

    From: /tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/sharepoint/comments/collection.py
  • class CommentInformation 72.1% similar

    A data class representing comment information in SharePoint, including the comment text and mentioned identities.

    From: /tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/sharepoint/comments/information.py
  • class LikedByInformation 71.8% similar

    Represents information about users who have liked a SharePoint list item, including like count, current user's like status, and collection of users who liked the item.

    From: /tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/sharepoint/likes/liked_by_information.py
  • class Identity_v1 68.7% similar

    Identity is a client value class representing a Microsoft SharePoint Comments identity entity, inheriting from ClientValue base class.

    From: /tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/sharepoint/comments/client/identity.py
  • class UserEntity 66.1% similar

    Represents a single like within a likedBy set of a SharePoint list item, providing access to like metadata such as creation date and user email.

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