🔍 Code Extractor

class User_v2

Maturity: 34

Represents a SharePoint Directory User entity with methods to check group membership and retrieve user's groups.

File:
/tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/sharepoint/directory/user.py
Lines:
6 - 35
Complexity:
moderate

Purpose

This class extends the Entity base class to provide SharePoint-specific user operations. It allows checking if a user is a member of a specific group and retrieving all groups the user belongs to. The class uses lazy loading patterns to ensure the principalName property is available before making directory queries through the SPHelper utility class.

Source Code

class User(Entity):
    def is_member_of(self, group_id):
        return_type = ClientResult(self.context)

        def _user_loaded():
            from office365.sharepoint.directory.helper import SPHelper

            SPHelper.is_member_of(
                self.context, self.properties["principalName"], group_id, return_type
            )

        self.ensure_property("principalName", _user_loaded)
        return return_type

    def get_my_groups(self):
        return_type = MyGroupsResult(self.context)

        def _user_loaded():
            from office365.sharepoint.directory.helper import SPHelper

            SPHelper.get_my_groups(
                self.context, self.properties["principalName"], 0, 10, return_type
            )

        self.ensure_property("principalName", _user_loaded)
        return return_type

    @property
    def entity_type_name(self):
        return "SP.Directory.User"

Parameters

Name Type Default Kind
bases Entity -

Parameter Details

Entity_base_class: Inherits from Entity base class which provides core entity functionality including context management, property handling, and ensure_property method for lazy loading

Return Value

Instantiation returns a User object representing a SharePoint directory user. The is_member_of method returns a ClientResult object containing boolean membership status. The get_my_groups method returns a MyGroupsResult object containing the user's group memberships. The entity_type_name property returns the string 'SP.Directory.User'.

Class Interface

Methods

is_member_of(self, group_id) -> ClientResult

Purpose: Checks if the user is a member of a specified group by group ID

Parameters:

  • group_id: The identifier (GUID) of the SharePoint group to check membership against

Returns: ClientResult object that will contain a boolean value indicating membership status after context.execute_query() is called

get_my_groups(self) -> MyGroupsResult

Purpose: Retrieves all groups that the user is a member of, with pagination (0-10 results)

Returns: MyGroupsResult object that will contain a collection of groups the user belongs to after context.execute_query() is called

@property entity_type_name(self) -> str property

Purpose: Returns the SharePoint entity type name for this user object

Returns: String literal 'SP.Directory.User' representing the SharePoint entity type

Attributes

Name Type Description Scope
context ClientContext Inherited from Entity base class, provides the SharePoint client context for executing queries instance
properties dict Inherited from Entity base class, stores entity properties including principalName used for directory operations instance

Dependencies

  • office365.runtime.client_result
  • office365.sharepoint.directory.my_groups_result
  • office365.sharepoint.entity
  • office365.sharepoint.directory.helper

Required Imports

from office365.runtime.client_result import ClientResult
from office365.sharepoint.directory.my_groups_result import MyGroupsResult
from office365.sharepoint.entity import Entity

Conditional/Optional Imports

These imports are only needed under specific conditions:

from office365.sharepoint.directory.helper import SPHelper

Condition: Imported lazily inside is_member_of and get_my_groups methods when these methods are called

Required (conditional)

Usage Example

# Assuming you have a SharePoint context and user object
from office365.sharepoint.directory.user import User

# User object is typically obtained from SharePoint context, not directly instantiated
# Example: user = context.web.current_user

# Check if user is member of a specific group
group_id = 'group-guid-here'
membership_result = user.is_member_of(group_id)
context.execute_query()
if membership_result.value:
    print('User is a member of the group')

# Get all groups the user belongs to
groups_result = user.get_my_groups()
context.execute_query()
for group in groups_result.value:
    print(f'Group: {group}')

Best Practices

  • Always call context.execute_query() after invoking is_member_of or get_my_groups to execute the deferred query
  • The User class uses lazy loading pattern - ensure principalName property is loaded before using methods
  • Do not instantiate User directly; obtain user objects through SharePoint context methods
  • Handle the ClientResult and MyGroupsResult objects properly by accessing their .value property after query execution
  • The ensure_property method ensures principalName is loaded before making directory queries, preventing null reference errors
  • Methods return result objects immediately but actual data is populated only after context.execute_query() is called

Similar Components

AI-powered semantic similarity - components with related functionality:

  • class Group_v1 85.6% similar

    Represents a SharePoint Directory Group entity that provides methods to retrieve group members, owners, and member information with lazy loading support.

    From: /tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/sharepoint/directory/group.py
  • class User_v1 76.8% similar

    Represents a user in Microsoft SharePoint Foundation, extending the Principal class to provide user-specific operations and properties.

    From: /tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/sharepoint/principal/users/user.py
  • class UserDirectoryInfo 70.5% similar

    A data class representing user information retrieved from a directory service, such as Active Directory or Azure AD, used in SharePoint sharing operations.

    From: /tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/sharepoint/sharing/user_directory_info.py
  • class SharePointDirectoryProvider 67.9% similar

    A SharePoint directory provider entity class that represents the SP.Directory.Provider.SharePointDirectoryProvider resource in SharePoint's REST API.

    From: /tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/sharepoint/directory/provider/provider.py
  • class GroupAndUserStatus 65.4% similar

    A SharePoint entity class representing the status of a group and user, providing access to associated Group information.

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