class RequestUserContext
Represents the user context for the current SharePoint request, providing access to user information and context details typically available at the /_api/me endpoint.
/tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/sharepoint/request_user_context.py
5 - 25
moderate
Purpose
This class serves as a wrapper for SharePoint's RequestUserContext entity, providing convenient property-based access to the current user context and user object. It inherits from Entity and implements lazy-loading properties that retrieve user-related information from SharePoint's REST API. The class is designed to be used when accessing user context information in SharePoint Online/On-Premises environments.
Source Code
class RequestUserContext(Entity):
"""The class that represents the user context for the present request. Typically found under /_api/me"""
@property
def current(self):
"""Gets the SP.RequestUserContext for the current request."""
return self.properties.get(
"Current",
RequestUserContext(
self.context, ResourcePath("Current", self.resource_path)
),
)
@property
def user(self):
"""The SP.User object for the current request."""
from office365.sharepoint.principal.users.user import User
return self.properties.get(
"User", User(self.context, ResourcePath("User", self.resource_path))
)
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
bases |
Entity | - |
Parameter Details
context: The client context object that manages the connection to SharePoint and handles API requests. This is inherited from the Entity base class and is required for making API calls.
resource_path: A ResourcePath object that specifies the API endpoint path for this entity. Inherited from Entity base class and typically points to the /_api/me endpoint or similar user context paths.
Return Value
Instantiation returns a RequestUserContext object that provides access to user context properties. The 'current' property returns another RequestUserContext instance representing the current request context. The 'user' property returns a User object containing detailed information about the current SharePoint user.
Class Interface
Methods
@property current(self) -> RequestUserContext
property
Purpose: Gets the SP.RequestUserContext for the current request, providing access to the current request's user context
Returns: A RequestUserContext instance representing the current request context, retrieved from cached properties or newly created with a 'Current' resource path
@property user(self) -> User
property
Purpose: Gets the SP.User object for the current request, providing access to detailed user information
Returns: A User object containing information about the current SharePoint user, retrieved from cached properties or newly created with a 'User' resource path
Attributes
| Name | Type | Description | Scope |
|---|---|---|---|
context |
ClientContext | The client context object inherited from Entity base class, used for making API requests to SharePoint | instance |
resource_path |
ResourcePath | The resource path object inherited from Entity base class, specifying the API endpoint for this user context entity | instance |
properties |
dict | Dictionary inherited from Entity base class that caches loaded properties and their values, used for lazy loading | instance |
Dependencies
office365.runtime.paths.resource_pathoffice365.sharepoint.entityoffice365.sharepoint.principal.users.user
Required Imports
from office365.runtime.paths.resource_path import ResourcePath
from office365.sharepoint.entity import Entity
Conditional/Optional Imports
These imports are only needed under specific conditions:
from office365.sharepoint.principal.users.user import User
Condition: only when accessing the 'user' property
OptionalUsage Example
from office365.sharepoint.client_context import ClientContext
from office365.runtime.auth.user_credential import UserCredential
from office365.sharepoint.request_user_context import RequestUserContext
# Authenticate and create context
site_url = 'https://yourtenant.sharepoint.com/sites/yoursite'
credentials = UserCredential('username', 'password')
ctx = ClientContext(site_url).with_credentials(credentials)
# Access user context (typically via ctx.me or similar)
user_context = ctx.me
ctx.load(user_context)
ctx.execute_query()
# Access current context
current_context = user_context.current
# Access user object
current_user = user_context.user
ctx.load(current_user)
ctx.execute_query()
print(f'User: {current_user.login_name}')
Best Practices
- Always ensure the client context is properly authenticated before accessing user context properties
- Use ctx.load() and ctx.execute_query() to fetch property values from SharePoint before accessing them
- Properties use lazy loading - they create objects on first access and cache them in the properties dictionary
- The 'current' property returns a nested RequestUserContext, useful for accessing the current request's context explicitly
- The 'user' property provides access to the full User object with all user-related properties and methods
- This class is typically accessed through the ClientContext.me property rather than instantiated directly
- Handle authentication errors gracefully as user context access requires valid credentials
- Be aware that property access may trigger API calls if the data hasn't been loaded yet
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
class RequestContext 66.8% similar
-
class ClientContext 65.4% similar
-
class QueryContext 65.1% similar
-
class User_v1 65.1% similar
-
class SharePointHomeServiceContextBuilder 63.7% similar