1

I have looked at:

How to get the parent folder name of Message with Exchangelib python

But have been unable to make this work using the following debugging code:

for item in docdead.all().order_by('-datetime_received')[:3000]: #look into the inbox the first 3K emails order desc by date received
    if item.datetime_received < ews_bfr: #if the mail if older than the custom date in the EWS format then apply rule
        print (item.subject)
        print (item.datetime_received)
        print (item.sender.email_address)
        print (item.sender.name)
        print (item.body)
        print(SingleFolderQuerySet(
            account=account,
            folder=account.root
        ).get(id=item.parent_folder_id.id))
        for attachment in item.attachments:
            print (attachment.name)

I get:

ValueError: EWS does not support filtering on field 'id'

I am sure its a simple error, but I would appreciate any help.

Iain
  • 31
  • 1

1 Answers1

1

If you're just querying one folder, then parent_folder_id will always point to that folder.

If you're querying multiple folders at a time, here's the general way to look up a folder name by ID:

from exchangelib.folders import FolderId, SingleFolderQuerySet

folder_name = SingleFolderQuerySet(
    account=account,
    folder=FolderId(id=item.parent_folder_id.id),
).resolve().name
Erik Cederstrand
  • 9,643
  • 8
  • 39
  • 63
  • Erik, thanks so much for providing this. I am getting an error on FolderId - is this a variable I need to set (based on the current sub folder ID?) and I am also getting an error on self.account rather than account. Apologies if I am asking naive question, what would code look like to get all messages and print the folder name of each? Thats my final goal, just trying to work it myself! – Iain Mar 13 '22 at 22:02
  • Sorry, fixed the example code now – Erik Cederstrand Mar 14 '22 at 06:20
  • Thanks Eric. I have altered my code and still getting ''folder_name = SingleFolderQuerySet( AttributeError: 'SingleFolderQuerySet' object has no attribute 'resolve'' I have imported: from exchangelib.folders import SingleFolderQuerySet, FolderId – Iain Mar 14 '22 at 20:26