🔍 Code Extractor

class DirectorySession

Maturity: 36

DirectorySession is a SharePoint directory service class that provides access to user information and graph data within a SharePoint context.

File:
/tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/sharepoint/directory/session.py
Lines:
7 - 40
Complexity:
moderate

Purpose

This class serves as an interface to SharePoint's directory services, enabling retrieval of user information from both SharePoint and Microsoft Graph. It provides methods to query user data by principal name or user ID, and offers access to the current user's information through the 'me' property. The class is designed to work within the Office365 SharePoint API framework and requires a context object for authentication and request management.

Source Code

class DirectorySession(Entity):
    def __init__(self, context):
        super(DirectorySession, self).__init__(
            context, ResourcePath("SP.Directory.DirectorySession")
        )

    @property
    def me(self):
        return self.properties.get(
            "Me", User(self.context, ResourcePath("Me", self.resource_path))
        )

    def get_graph_user(self, principal_name):
        """
        :type principal_name: str
        """
        return_type = User(self.context)
        qry = ServiceOperationQuery(
            self, "GetGraphUser", [principal_name], None, None, return_type
        )
        self.context.add_query(qry)
        return return_type

    def get_sharepoint_data_for_user(self, user_id):
        return_type = User(self.context)
        qry = ServiceOperationQuery(
            self, "GetSharePointDataForUser", [user_id], None, None, return_type
        )
        self.context.add_query(qry)
        return return_type

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

Parameters

Name Type Default Kind
bases Entity -

Parameter Details

context: A SharePoint context object that manages authentication, request execution, and API communication. This context is required for all operations and is passed to parent Entity class and used for query execution.

Return Value

Instantiation returns a DirectorySession object that can be used to query SharePoint directory services. The get_graph_user and get_sharepoint_data_for_user methods return User objects that will be populated with data after the context executes pending queries. The 'me' property returns a User object representing the current authenticated user.

Class Interface

Methods

__init__(self, context)

Purpose: Initializes a DirectorySession instance with the provided SharePoint context

Parameters:

  • context: SharePoint context object for authentication and query execution

Returns: None (constructor)

@property me(self) -> User property

Purpose: Returns a User object representing the current authenticated user

Returns: User object for the current authenticated user, lazily loaded from properties or created with 'Me' resource path

get_graph_user(self, principal_name: str) -> User

Purpose: Retrieves user information from Microsoft Graph by principal name (email address)

Parameters:

  • principal_name: The user's principal name (typically email address) to query in Microsoft Graph

Returns: User object that will be populated with graph data after context.execute_query() is called

get_sharepoint_data_for_user(self, user_id: str) -> User

Purpose: Retrieves SharePoint-specific user data by user ID

Parameters:

  • user_id: The unique identifier for the user in SharePoint

Returns: User object that will be populated with SharePoint user data after context.execute_query() is called

@property entity_type_name(self) -> str property

Purpose: Returns the SharePoint entity type identifier for this class

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

Attributes

Name Type Description Scope
context ClientContext SharePoint context object inherited from Entity base class, used for authentication and query execution instance
resource_path ResourcePath Resource path object inherited from Entity, set to 'SP.Directory.DirectorySession' for API routing instance
properties dict Dictionary inherited from Entity that stores cached property values like the 'me' User object instance

Dependencies

  • office365

Required Imports

from office365.runtime.paths.resource_path import ResourcePath
from office365.runtime.queries.service_operation import ServiceOperationQuery
from office365.sharepoint.directory.user import User
from office365.sharepoint.entity import Entity

Usage Example

from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.directory.session import DirectorySession

# Create SharePoint context with authentication
ctx = ClientContext('https://yourtenant.sharepoint.com').with_credentials(username, password)

# Create directory session
dir_session = DirectorySession(ctx)

# Get current user information
current_user = dir_session.me
ctx.load(current_user)
ctx.execute_query()
print(current_user.properties)

# Get user by principal name
user = dir_session.get_graph_user('user@domain.com')
ctx.execute_query()
print(user.properties)

# Get SharePoint data for specific user
user_data = dir_session.get_sharepoint_data_for_user('user_id_123')
ctx.execute_query()
print(user_data.properties)

Best Practices

  • Always call ctx.execute_query() after calling get_graph_user or get_sharepoint_data_for_user to actually execute the pending queries and populate the returned User objects
  • The DirectorySession requires a valid authenticated context object; ensure proper authentication is configured before instantiation
  • User objects returned by methods are initially empty and only populated after execute_query() is called on the context
  • The 'me' property uses lazy loading - the User object is created on first access and cached in the properties dictionary
  • Multiple queries can be batched by calling multiple methods before executing ctx.execute_query() for better performance
  • Handle authentication errors and permission issues when accessing directory services
  • The entity_type_name property returns the SharePoint type identifier and should not be modified

Similar Components

AI-powered semantic similarity - components with related functionality:

  • class SharePointDirectoryProvider 70.2% 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 UserDirectoryInfo 68.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 User_v2 65.3% similar

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

    From: /tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/sharepoint/directory/user.py
  • class Group_v1 63.5% 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 DirectoryObject 61.6% similar

    Represents an Azure Active Directory object, serving as the base type for directory entities like users, groups, service principals, and organizational contacts.

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