class DirectorySession
DirectorySession is a SharePoint directory service class that provides access to user information and graph data within a SharePoint context.
/tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/sharepoint/directory/session.py
7 - 40
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
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
class SharePointDirectoryProvider 70.2% similar
-
class UserDirectoryInfo 68.5% similar
-
class User_v2 65.3% similar
-
class Group_v1 63.5% similar
-
class DirectoryObject 61.6% similar