Skip to content

Added docs for query builder #1173

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions docs/source/usage/utils/query.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,53 @@
Query
=====

.. _query_builder:

Query Builder
-------------

A query can be created for every ``ApiComponent`` (such as ``MailBox``). The ``Query`` can be used to handle the filtering, sorting, selecting, expanding and search very easily.

For example:

.. code-block:: python

from O365.utils import ExperimentalQuery
builder = ExperimentalQuery(protocol=account.protocol)

query = builder.chain_or(builder.contains('subject', 'george best'), builder.startswith('subject', 'quotes')

# 'created_date_time' will automatically be converted to the protocol casing.
# For example when using MS Graph this will become 'createdDateTime'.

query = query & builder.greater('created_date_time', datetime(2018, 3, 21))

print(query)

# contains(subject, 'george best') or startswith(subject, 'quotes') and createdDateTime gt '2018-03-21T00:00:00Z'
# note you can pass naive datetimes and those will be converted to you local timezone and then send to the api as UTC in iso8601 format

# To use Query objetcs just pass it to the query parameter:
filtered_messages = mailbox.get_messages(query=query)

You can also specify specific data to be retrieved with "select":

.. code-block:: python

# select only some properties for the retrieved messages:
query = builder.select('subject', 'to_recipients', 'created_date_time)

messages_with_selected_properties = mailbox.get_messages(query=query)

You can also search content. As said in the graph docs:

You can currently search only message and person collections. A $search request returns up to 250 results. You cannot use $filter or $orderby in a search request.

If you do a search on messages and specify only a value without specific message properties, the search is carried out on the default search properties of from, subject, and body.

.. code-block:: python

# searching is the easy part ;)
query = builder.search('george best is da boss')
messages = mailbox.get_messages(query=query)

4 changes: 4 additions & 0 deletions docs/source/usage/utils/utils.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ For example:

Query helper
------------
.. note::

This method of creating queries is now deprecated, queries shoould now be created using the ExperimentalQuery methods - :ref:`query_builder`

Every ``ApiComponent`` (such as ``MailBox``) implements a new_query method that will return a ``Query`` instance. This ``Query`` instance can handle the filtering, sorting, selecting, expanding and search very easily.

For example:
Expand Down