O365 / python-o365

A simple python library to interact with Microsoft Graph and Office 365 API
Apache License 2.0
1.68k stars 424 forks source link

OrderBy does not work #260

Closed k8uwall closed 5 years ago

k8uwall commented 5 years ago

Hi, thanks for making this very great library.

I tried to line them in reverse order, but order_by didn't work.

account = Account((config['client_id'], config['client_secret']))
mailbox = account.mailbox()
inbox = mailbox.inbox_folder()
for message in inbox.get_messages(order_by='DateTimeReceived desc'):
    print(message)
requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: https://graph.microsoft.com/v1.0/me/mailFolders/Inbox/messages?%24top=25&%24orderby=DateTimeReceived+desc | Error Message: OrderBy not supported.

The reason seems to be that space is encoded into +. I think it will work if it becomes %20. I tried, but I didn't know how to fix it.

alejcas commented 5 years ago

Can you try:

account = Account((config['client_id'], config['client_secret']))
mailbox = account.mailbox()
inbox = mailbox.inbox_folder()
query = inbox.q().order_by('DateTimeReceived')
for message in inbox.get_messages(query=query):
    print(message)

Is this working?

k8uwall commented 5 years ago

I tried it.

account = Account((config['client_id'], config['client_secret']))
mailbox = account.mailbox()
inbox = mailbox.inbox_folder()
query = inbox.q().order_by('DateTimeReceived')
for message in inbox.get_messages(query=query):
    print(message)
requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: https://graph.microsoft.com/v1.0/me/mailFolders/Inbox/messages?%24top=25&%24orderby=dateTimeReceived | Error Message: OrderBy not supported.

HMM...

aufbakanleitung commented 5 years ago

I have the same issue. I've also tried to order the results after the query but that didn't work either.

mailbox = account.mailbox().inbox_folder()
query = mailbox.new_query().on_attribute('is_read').equals(False)
messages = mailbox.get_messages(limit=amount, query=query, download_attachments=True).order_by('DateTimeReceived')
# AttributeError: 'generator' object has no attribute 'order_by'
aufbakanleitung commented 5 years ago

This didn't work either.

messages = mailbox.get_messages(limit=amount, query=query, order_by='DateTimeReceived', download_attachments=True)
# 400 Client Error: Bad Request for url: [..] | Error Message: OrderBy not supported.
aufbakanleitung commented 5 years ago

Aha, if I try to query on order_by="DateTimeReceived" directly in MS Graph it also gives the error OrderBy not supported. So by way of testing I tried to order_by a variable that is clearly available, and then it does work:

def get_new_email(amount):
    mailbox = account.mailbox().inbox_folder()
    query = mailbox.new_query().on_attribute('is_read').equals(False)  # Get only unread mail
    messages = mailbox.get_messages(limit=amount, query=query, order_by='subject', download_attachments=True)  # Apply query
    return messages

So we just have to find out with what command MS Graph can order by DateTime with.

aufbakanleitung commented 5 years ago

Looking at debugging I would think it should be possible to order_by='received' but alas this returns the same Error Message: OrderBy not supported.

image
k8uwall commented 5 years ago

My question was wrong.

I want to get these results in date descending order

Subject: Schedule December 24, 2018 Subject: Schedule November 23, 2018 Subject: Schedule November 3, 2018

If you execute the following code, it will be output in ascending order.

query = inbox.q().on_attribute('subject').contains("Schedule")
for message in inbox.get_messages(query=query):
    print(message)

Subject: Schedule November 3, 2017 Subject: Schedule November 23, 2017 Subject: Schedule December 24, 2017

Specifying descending order causes an error.

query = inbox.q().on_attribute('subject').contains("Schedule")
order_by='DateTimeReceived desc'
for message in inbox.get_messages(query=query, order_by=order_by):
    print(message)
requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: https://graph.microsoft.com/v1.0/me/mailFolders/Inbox/messages?%24top=25&%24orderby=DateTimeReceived+desc | Error Message: OrderBy not supported.

I used a 'journal rules' of outlook

mailbox = account.mailbox()
schedule_folder = mailbox.inbox_folder().get_folder(folder_name='schedule')
for message in inbox.get_messages():
    print(message)

I am sorry for making Issues dirty.

k8uwall commented 5 years ago

I posted it without noticing the reply. I wonder if it was good to close it.

aufbakanleitung commented 5 years ago

Ah too bad, because I'm still struggling to retrieve the e-mails in chronological order (FiFo). I'll just open a new ticket then.

k8uwall commented 5 years ago

If python can complete it, I would like to do so, so I will watch it.

alejcas commented 5 years ago

I think it’s “receivedDateTime”