I am trying to use exchangelib to retrieve messages from a account that match\starts with a particular subject. This must include messages from all folders and not just the 'inbox' folder. I went through a couple of similar questions here
How can I get the latest emails from all folders with exchangelib?
which led me to use FolderCollection. However, I am not clear how create and pass a list of all folders to the 'folders' parameter.
Currently I tried the below:
conf = Configuration(server=ex_srv, credentials=cred)
acct = Account(primary_smtp_address=email,
config=conf,
autodiscover=False,
credentials=cred,
access_type=IMPERSONATION
)
from exchangelib.folders import Messages, FolderCollection
all_folder_list = acct.folders[Messages]
all_folder_collection = FolderCollection(account=acct, folders=all_folder_list)
all_folder_collection.filter(subject__startswith='test')
I seem to be making a silly error here, but cannot get around to catching it.
Could someone please let me know the most optimal way of achieving this use case?
Edit:
The error I see is
Traceback (most recent call last):
File "queryset.py", line 298, in __iter__
for val in self._format_items(items=self._query(), return_format=self.return_format):
File "queryset.py", line 375, in _item_yielder
for i in iterable:
File "account.py", line 580, in fetch
shape=ID_ONLY,
File "account.py", line 302, in _consume_item_service
is_empty, items = peek(items)
File "util.py", line 118, in peek
first = next(iterable)
File "folders.py", line 250, in find_items
for i in items:
File "services.py", line 432, in _paged_call
parsed_pages = [self._get_page(message) for message in response]
File "services.py", line 432, in <listcomp>
parsed_pages = [self._get_page(message) for message in response]
File "services.py", line 481, in _get_page
rootfolder = self._get_element_container(message=message, name='{%s}RootFolder' % MNS)
File "services.py", line 345, in _get_element_container
raise self._get_exception(code=response_code, text=msg_text, msg_xml=msg_xml)
exchangelib.errors.ErrorInvalidOperation: Shared folder search cannot be performed on multiple folders.