class MailSearchFolder
A virtual folder class representing a mail search folder in Microsoft Exchange Online that contains email items matching specified search criteria.
/tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/outlook/mail/folders/search.py
4 - 11
moderate
Purpose
MailSearchFolder provides a specialized implementation of MailFolder for creating and managing virtual search folders in Exchange Online mailboxes. These folders dynamically contain email items that match defined search criteria. While search folders can be created in any mailbox folder, they must be created in the WellKnownFolderName.SearchFolders location to appear in Outlook, Outlook for the web, or Outlook Live. This class inherits all functionality from MailFolder and extends it for search-specific operations.
Source Code
class MailSearchFolder(MailFolder):
"""
A mailSearchFolder is a virtual folder in the user's mailbox that contains all the email items
matching specified search criteria. mailSearchFolder inherits from mailFolder.
Search folders can be created in any folder in a user's Exchange Online mailbox. However, for a search folder
to appear in Outlook, Outlook for the web, or Outlook Live, the folder must be created in the
WellKnownFolderName.SearchFolders folder.
"""
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
bases |
MailFolder | - |
Parameter Details
__init__: The constructor parameters are inherited from the parent MailFolder class. Typically includes context for API communication and optional parent folder reference.
Return Value
Instantiation returns a MailSearchFolder object that represents a virtual search folder. Methods inherited from MailFolder return various types including folder metadata, message collections, and operation results depending on the specific method called.
Class Interface
Methods
__init__(context, resource_path=None)
Purpose: Initializes a new MailSearchFolder instance (inherited from MailFolder)
Parameters:
context: The client context for API communication with Microsoft Graphresource_path: Optional resource path for the folder in the API
Returns: A new MailSearchFolder instance
Attributes
| Name | Type | Description | Scope |
|---|---|---|---|
display_name |
str | The display name of the search folder (inherited from MailFolder) | instance |
source_folder_ids |
list[str] | Collection of folder IDs that define the scope of the search | instance |
filter_query |
str | OData filter query string that defines the search criteria for emails | instance |
messages |
MessageCollection | Collection of messages that match the search criteria (inherited from MailFolder) | instance |
id |
str | Unique identifier for the search folder (inherited from MailFolder) | instance |
parent_folder_id |
str | The ID of the parent folder containing this search folder (inherited from MailFolder) | instance |
Dependencies
office365
Required Imports
from office365.outlook.mail.folders.search_folder import MailSearchFolder
from office365.outlook.mail.folders.folder import MailFolder
Usage Example
from office365.runtime.auth.client_credential import ClientCredential
from office365.graph_client import GraphClient
from office365.outlook.mail.folders.search_folder import MailSearchFolder
# Authenticate
credentials = ClientCredential(client_id, client_secret)
client = GraphClient(credentials)
# Get the search folders container
search_folders = client.me.mail_folders.get_by_name('SearchFolders').execute_query()
# Create a new search folder
new_search_folder = MailSearchFolder(client.me.mail_folders.context)
new_search_folder.display_name = 'Important Emails'
new_search_folder.source_folder_ids = ['inbox_folder_id']
new_search_folder.filter_query = 'importance eq high'
client.me.mail_folders.add(new_search_folder).execute_query()
# Access the created search folder
my_search_folder = client.me.mail_folders.get_by_id(new_search_folder.id).execute_query()
messages = my_search_folder.messages.get().execute_query()
Best Practices
- Always create search folders in the WellKnownFolderName.SearchFolders location for visibility in Outlook clients
- Ensure proper authentication and permissions (Mail.ReadWrite or Mail.ReadWrite.Shared) before creating or modifying search folders
- Define clear and efficient filter queries to avoid performance issues with large mailboxes
- Use the inherited methods from MailFolder for standard folder operations like listing messages, updating properties, or deleting
- Remember that search folders are virtual - they don't physically contain messages but dynamically display results based on criteria
- Handle API rate limits and implement retry logic for production applications
- Always call execute_query() after operations to commit changes to the server
- Clean up unused search folders to maintain mailbox organization and performance
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
class MailFolder 71.9% similar
-
class MailFolderCollection 63.3% similar
-
class SearchEntity 52.1% similar
-
function search_for_folders 51.4% similar
-
class CollaborationMailbox 51.3% similar