Package: python-msgraph
Python-MSGraphpython-msgraphis a Python wrapper of theMicrosoft GraphAPI.InstallationTo install thepython-msgraphlibrary use the following command:python-mpipinstallpython-msgraphor, to build locally and install:UsageAuthenticationThe library currently supports connecting to the API using an SSL certificate:frommsgraphimportapiauthority_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 aclient_certificateis changed, theclient_thumbprintandclient_idvalues must also be changedUsing the API to fetch UsersYou can use themsgraph.usermodule to interact withUserinstances.Userinstanced can be fetched using themsgraph.user.Userclass:frommsgraphimportuserall_users=user.User.get(api_instance)To fetch a specific user, you can also include the user'sUser Principal Name, which is the user's email address:johndoe_instance=user.User.get(api_instance,user='johndoe@wm.edu')Calendars & EventsFetch a User's CalendarsNow let's fetch theCalendars of a particular user.  To interact with  aCalendar,Event, calendarGroup, or  calendarCategoryinstance, we will use themsgraph.calendarmodule:frommsgraphimportcalendarjohndoe_calendars=calendar.Calendar.get(api_instance,user=johndoe_instance)Fetch a User's Events from a given CalendarNow let's fetch theEventinstances from the main calendar ofjohndoe:calendar_lookup=dict()forcalendarinjohndoe_calendars:calendar_lookup[calendar.name]=calendarprimary_calendar=calendar_lookup['Calendar']johndoe_events=calendar.Event.get(johndoe_instance,calendar=primary_calendar)Update an EventTo update anEvent, we can use theEvent.updatemethod:johndoe_event=johndoe_events[0]johndoe_event.subject='Important meeting'johndoe_event.update(api_instance)Now the updates made to theEventobject have been saved back to thecalendarofjohndoe.Delete an Event from a CalendarLet's try deleting anEventon aCalendarusing theEvent.deletemethod:johndoe_event=johndoe_events[0]johndoe_event.delete(api_instance)After calling thedeletemethod, theEventhas been removed from thecalendarofjohndoe.Sharepoint Sites & ListsSearch for a siteTo fetch all sites matching a key phrase, use themsgraph.sites.Site.searchmethod:frommsgraphimportsitesmatching_sites=sites.Site.search(api_instance,'software')Fetching a specific siteSpecificmsgraph.sites.Siteinstances 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 sitesSharePoint sites can have sub-sites within them.  To get the subsites of a Sharepoint site, use themsgraph.sites.Site.subsitesmethod:subsites=site.subsites(api_instance)To traverse the hierarchy ofmsgraph.sites.Siteinstances:defbreadth_first(api,root):queue=[root]whilequeue:site=queue.pop(0)subsites=site.subsites(api)queue+=subsitesreturnqueuebreadth_first_hierarchy=breadth_first(api_instance,site)defdepth_first(api,root):queue=[root]whilequeue:site=queue.pop()subsites=site.subsites(api)queue+=subsitesreturnqueuedepth_first_hierarchy=breadth_first(api_instance,site)Fetching Listsmsgraph.sites.SiteListinstances can be fetched using themsgraph.sites.SiteList.getmethod:site_lists=sites.SiteList.get(api_instance,site)Or, if you have the ID of themsgraph.sites.SiteList, you can specify it as alist_instancekeyword argument to fetch the specificmsgraph.sites.SiteListinstance:list_id='XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX'site_list=sites.SiteList.get(api_instance,site,list_instance=list_id)NOTE:  Allsitemethod 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 SiteListsTo fetch themsgraph.sites.ListIteminstances for amsgraph.sites.SiteList, use themsgraph.sites.ListItem.getmethod:list_items=sites.ListItem.get(api_instance,site,site_list)Fetching previous versions of ListItemsmsgraph.sites.ListIteminstances can be updated in Microsoft Graph.  To fetch the previous versions, use themsgraph.sites.ListItem.versionsmethod:foriteminlist_items:previous_versions=item.versions(api_instance,site,site_list)NOTE:  Allsiteandsite_listmethod 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)foriteminlist_items:previous_versions=item.versions(api_instance,site_id,list_id)Creating a ListItemTo create a newmsgraph.sites.ListItemuse themsgraph.sites.ListItem.createmethod:new_list_item_fields=dict(Title='Programmer')new_list_item=sites.ListItem.create(api_instance,site,list,new_list_item_fields)Updating a ListItemTo update the properties of amsgraph.sites.ListIteminstance, use themsgraph.sites.ListItem.updatemethod:forindex,iteminenumerate(list_items):item.name='%s#%i'%(item.name,index)item.update(api_instance,site,site_list)To update the fields of amsgraph.sites.ListIteminstance, use themsgraph.sites.ListItem.update_fieldsmethod:forindex,iteminenumerate(list_items):item['Title']='Assistant Executive '+item['Title']item.update_fields(api_instance,site,site_list)or alternatively:forindex,iteminenumerate(list_items):fields=dict(Title='Assistant Executive '+item['Title'])item.update_fields(api_instance,site,site_list,fields=fields)NOTE:  Allsiteandsite_listmethod parameters can be substituted with their IDs.  So the code below would be valid:foriteminlist_items:item['Title']='Assistant Executive '+item['Title']item.update_fields(api_instance,site_id,list_id)Deleting a ListItemTo delete an existingmsgraph.sites.ListIteminstance, use themsgraph.sites.ListItem.deletemethod:new_list_item.delete(api_instance,site,site_list)LoggingThe 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