class SharingUtility
A utility class for SharePoint sharing operations that provides static methods to retrieve user directory information and validate user email addresses.
/tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/sharepoint/sharing/utility.py
8 - 54
moderate
Purpose
SharingUtility is a specialized Entity class that encapsulates SharePoint sharing-related operations. It provides static utility methods for querying user directory information by email and validating that multiple email addresses belong to the same user. This class is designed to work within the Office365 SharePoint client context and uses service operation queries to interact with SharePoint's sharing APIs.
Source Code
class SharingUtility(Entity):
"""Provides sharing related utility methods."""
def __init__(self, context):
super(SharingUtility, self).__init__(context, ResourcePath("SharingUtility"))
@staticmethod
def get_user_directory_info_by_email(context, email):
"""
Get user information by the user’s email address in directory.
:param str email: The email address of a user.
:param office365.sharepoint.client_context.ClientContext context: SharePoint client context
"""
return_type = ClientResult(context, UserDirectoryInfo())
payload = {"email": email}
utility = SharingUtility(context)
qry = ServiceOperationQuery(
utility, "GetUserDirectoryInfoByEmail", None, payload, None, return_type
)
qry.static = True
context.add_query(qry)
return return_type
@staticmethod
def validate_same_user_emails(context, primary_email, other_email, principal_name):
"""
Validate the primary email/principal name and other email are of the same user.
:param str primary_email: User’s primary email address
:param str other_email: Another email address.
:param str principal_name: User’s principal name.
:param office365.sharepoint.client_context.ClientContext context: SharePoint client context
"""
utility = SharingUtility(context)
payload = {
"primaryEmail": primary_email,
"otherEmail": other_email,
"principalName": principal_name,
}
result = ClientResult(context)
qry = ServiceOperationQuery(
utility, "ValidateSameUserEmails", None, payload, None, result
)
qry.static = True
context.add_query(qry)
return result
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
bases |
Entity | - |
Parameter Details
context: An instance of office365.sharepoint.client_context.ClientContext that provides the connection and authentication context for SharePoint operations. This is required for all SharePoint API interactions and manages the communication with the SharePoint server.
Return Value
The constructor returns a SharingUtility instance initialized with the provided context and a ResourcePath of 'SharingUtility'. The static methods return ClientResult objects that will contain the query results after execution: get_user_directory_info_by_email returns a ClientResult containing UserDirectoryInfo, and validate_same_user_emails returns a ClientResult with validation results.
Class Interface
Methods
__init__(self, context) -> None
Purpose: Initializes a SharingUtility instance with the provided SharePoint client context
Parameters:
context: office365.sharepoint.client_context.ClientContext instance for SharePoint operations
Returns: None - initializes the instance with parent Entity class and ResourcePath('SharingUtility')
get_user_directory_info_by_email(context, email) -> ClientResult
static
Purpose: Retrieves user directory information for a user identified by their email address from SharePoint
Parameters:
context: office365.sharepoint.client_context.ClientContext - SharePoint client context for the operationemail: str - The email address of the user to look up in the directory
Returns: ClientResult containing UserDirectoryInfo object with user details (display name, principal name, etc.) after execute_query() is called
validate_same_user_emails(context, primary_email, other_email, principal_name) -> ClientResult
static
Purpose: Validates whether a primary email/principal name and another email address belong to the same user in SharePoint directory
Parameters:
context: office365.sharepoint.client_context.ClientContext - SharePoint client context for the operationprimary_email: str - The user's primary email addressother_email: str - Another email address to validate against the primaryprincipal_name: str - The user's principal name (typically the primary email in UPN format)
Returns: ClientResult containing a boolean value indicating whether the emails belong to the same user, available after execute_query() is called
Attributes
| Name | Type | Description | Scope |
|---|---|---|---|
context |
office365.sharepoint.client_context.ClientContext | Inherited from Entity parent class - stores the SharePoint client context used for API operations | instance |
resource_path |
ResourcePath | Inherited from Entity parent class - stores the resource path 'SharingUtility' identifying this entity in SharePoint API | instance |
Dependencies
office365
Required Imports
from office365.runtime.client_result import ClientResult
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.sharing.user_directory_info import UserDirectoryInfo
Usage Example
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.sharing.utilities.sharing_utility import SharingUtility
# Initialize SharePoint context with credentials
ctx = ClientContext('https://yourtenant.sharepoint.com/sites/yoursite').with_credentials(user_credentials)
# Get user directory information by email
user_info_result = SharingUtility.get_user_directory_info_by_email(ctx, 'user@example.com')
ctx.execute_query()
user_info = user_info_result.value
print(f"User display name: {user_info.display_name}")
# Validate that two emails belong to the same user
validation_result = SharingUtility.validate_same_user_emails(
ctx,
primary_email='user@example.com',
other_email='user.alias@example.com',
principal_name='user@example.com'
)
ctx.execute_query()
is_same_user = validation_result.value
print(f"Emails belong to same user: {is_same_user}")
Best Practices
- Always call ctx.execute_query() after invoking static methods to execute the queued service operations and retrieve results
- The static methods add queries to the context but do not execute them immediately - results are available only after execute_query() is called
- Ensure the ClientContext has appropriate permissions to access user directory information before calling these methods
- Handle potential exceptions that may occur during query execution, such as authentication failures or permission errors
- The class itself is rarely instantiated directly by users - the static methods create temporary instances internally
- ClientResult objects returned by methods will have their 'value' property populated only after execute_query() completes successfully
- Both static methods are thread-safe as they create new utility instances for each call
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
class Utility 79.4% similar
-
class Utility_v1 74.5% similar
-
class UserDirectoryInfo 68.6% similar
-
class ObjectSharingInformationUser 68.5% similar
-
class SPHelper 67.7% similar