Python wrapper to the Microsoft Graph API
Project description
Python-MSGraph
python-msgraph is a Python wrapper of the Microsoft Graph API.
Installation
To install the python-msgraph library use the following command:
python -m pip install python-msgraph
or, to build locally and install:
Usage
Authentication
The library currently supports connecting to the API using an SSL certificate:
from msgraph import api
authority_host_uri = 'https://login.microsoftonline.com'
tenant = 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX'
resource_uri = 'https://graph.microsoft.com'
client_id = 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX'
client_thumbprint = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
client_certificate = ''
api_instance = api.GraphAPI.from_certificate(authority_host_uri, tenant, resource_uri, client_id, client_certificate, client_thumbprint)
NOTE: When a client_certificate is changed, the client_thumbprint and client_id values must also be changed
Using the API to fetch Users
You can use the msgraph.user module to interact with User instances. User instanced can be fetched using the msgraph.user.User class:
from msgraph import user
all_users = user.User.get(api_instance)
To fetch a specific user, you can also include the user's User Principal Name, which is the user's email address:
johndoe_instance = user.User.get(api_instance, user='johndoe@wm.edu')
Calendars & Events
Fetch a User's Calendars
Now let's fetch the Calendars of a particular user. To interact with a Calendar, Event, calendar Group, or calendar Category instance, we will use the msgraph.calendar module:
from msgraph import calendar
johndoe_calendars = calendar.Calendar.get(api_instance, user=johndoe_instance)
Fetch a User's Events from a given Calendar
Now let's fetch the Event instances from the main calendar of johndoe:
calendar_lookup = dict()
for calendar in johndoe_calendars:
calendar_lookup[calendar.name] = calendar
primary_calendar = calendar_lookup['Calendar']
johndoe_events = calendar.Event.get(johndoe_instance, calendar=primary_calendar)
Update an Event
To update an Event, we can use the Event.update method:
johndoe_event = johndoe_events[0]
johndoe_event.subject = 'Important meeting'
johndoe_event.update(api_instance)
Now the updates made to the Event object have been saved back to the calendar of johndoe.
Delete an Event from a Calendar
Let's try deleting an Event on a Calendar using the Event.delete method:
johndoe_event = johndoe_events[0]
johndoe_event.delete(api_instance)
After calling the delete method, the Event has been removed from the calendar of johndoe.
Sharepoint Sites & Lists
Search for a site
To fetch all sites matching a key phrase, use the msgraph.sites.Site.search method:
from msgraph import sites
matching_sites = sites.Site.search(api_instance, 'software')
Fetching a specific site
Specific msgraph.sites.Site instances can be fetched using multiple methods:
-
msgraph.sites.Site.getmethod fetches sites by using the ID of the sitesite_id = 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX' site = sites.Site.get(api_instance, site=site_id)
-
msgraph.sites.Site.by_relative_urlmethod to fetch by the host name and relative url of the SharePoint sitehost_name = '' relative_url = '' site = sites.Site.by_relative_url(api_instance, host_name, relative_url)
-
msgraph.sites.Site.by_groupmethod fetches the team site of a given groupgroup_id = 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX' site = sites.Site.by_group(api_instance, group=group_id)
Traversing hierarchy of sites
SharePoint sites can have sub-sites within them. To get the subsites of a Sharepoint site, use the msgraph.sites.Site.subsites method:
subsites = site.subsites(api_instance)
To traverse the hierarchy of msgraph.sites.Site instances:
def breadth_first(api, root):
queue = [root]
while queue:
site = queue.pop(0)
subsites = site.subsites(api)
queue += subsites
return queue
breadth_first_hierarchy = breadth_first(api_instance, site)
def depth_first(api, root):
queue = [root]
while queue:
site = queue.pop()
subsites = site.subsites(api)
queue += subsites
return queue
depth_first_hierarchy = breadth_first(api_instance, site)
Fetching Lists
msgraph.sites.SiteList instances can be fetched using the msgraph.sites.SiteList.get method:
site_lists = sites.SiteList.get(api_instance, site)
Or, if you have the ID of the msgraph.sites.SiteList, you can specify it as a list_instance keyword argument to fetch the specific msgraph.sites.SiteList instance:
list_id = 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX'
site_list = sites.SiteList.get(api_instance, site, list_instance=list_id)
NOTE: All site method parameters can be substituted with their IDs in the examples above. So the code below would be valid:
site_list = sites.SiteList.get(api_instance, site_id, list_instance=list_id)
Fetching ListItems for SiteLists
To fetch the msgraph.sites.ListItem instances for a msgraph.sites.SiteList, use the msgraph.sites.ListItem.get method:
list_items = sites.ListItem.get(api_instance, site, site_list)
Fetching previous versions of ListItems
msgraph.sites.ListItem instances can be updated in Microsoft Graph. To fetch the previous versions, use the msgraph.sites.ListItem.versions method:
for item in list_items:
previous_versions = item.versions(api_instance, site, site_list)
NOTE: All site and site_list method parameters can be substituted with their IDs. So the code below would be valid:
list_items = sites.ListItem.get(api_instance, site_id, list_id)
for item in list_items:
previous_versions = item.versions(api_instance, site_id, list_id)
Creating a ListItem
To create a new msgraph.sites.ListItem use the msgraph.sites.ListItem.create method:
new_list_item_fields = dict(Title='Programmer')
new_list_item = sites.ListItem.create(api_instance, site, list, new_list_item_fields)
Updating a ListItem
To update the properties of a msgraph.sites.ListItem instance, use the msgraph.sites.ListItem.update method:
for index, item in enumerate(list_items):
item.name = '%s #%i' % (item.name, index)
item.update(api_instance, site, site_list)
To update the fields of a msgraph.sites.ListItem instance, use the msgraph.sites.ListItem.update_fields method:
for index, item in enumerate(list_items):
item['Title'] = 'Assistant Executive ' + item['Title']
item.update_fields(api_instance, site, site_list)
or alternatively:
for index, item in enumerate(list_items):
fields = dict(Title='Assistant Executive ' + item['Title'])
item.update_fields(api_instance, site, site_list, fields=fields)
NOTE: All site and site_list method parameters can be substituted with their IDs. So the code below would be valid:
for item in list_items:
item['Title'] = 'Assistant Executive ' + item['Title']
item.update_fields(api_instance, site_id, list_id)
Deleting a ListItem
To delete an existing msgraph.sites.ListItem instance, use the msgraph.sites.ListItem.delete method:
new_list_item.delete(api_instance, site, site_list)
Logging
The following modules have their own loggers:
msgraph.api- Used for logging error messages from theAPIand logging rawHTTPresponse contentmsgraph.calendar- Used for logging the creation/update/deletes ofmsgraph.calendar.Calendar/msgraph.calendar.Event/msgraph.calendar.msgraph.calendar.Group/msgraph.calendar.Categoryinstancesmsgraph.group- Used for logging the creation/update/deletes ofmsgraph.group.Groupinstancesmsgraph.site- Used for logging the creation/update/deletes ofmsgraph.sites.Siteinstances,msgraph.sites.SiteListinstances, andmsgraph.sites.ListIteminstancesmsgraph.user- Used for logging the creation/update/deletes ofmsgraph.user.Userinstances
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file python-msgraph-0.2.8.tar.gz.
File metadata
- Download URL: python-msgraph-0.2.8.tar.gz
- Upload date:
- Size: 20.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.12.0 setuptools/44.1.1 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/2.7.17
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c1da406e9eb79ccf61e61b8a6f85618b4f58acd891ded33eaaeaa01cf6c0c200
|
|
| MD5 |
8ffd8bd48fef537ead4d06fe2b0b0877
|
|
| BLAKE2b-256 |
046426df9615a4495824e93dcd01f76a948a548e22a40a524eb6e024a10f434a
|
File details
Details for the file python_msgraph-0.2.8-py2.py3-none-any.whl.
File metadata
- Download URL: python_msgraph-0.2.8-py2.py3-none-any.whl
- Upload date:
- Size: 25.6 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.12.0 setuptools/44.1.1 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/2.7.17
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7bc517b0c1827f02edc6c3539a7352d33ee22db162af16bff5cbd6b702e5d076
|
|
| MD5 |
3cbd68492aa50d174ef753e318c2f190
|
|
| BLAKE2b-256 |
1b0c4792e84395e8b2d05bc1df70f7f6164d2072d478a62f5881d4f115f8c028
|