1
1
# Python-MSGraph
2
- ` python-msgraph ` is a Python wrapper of the [ Microsoft Graph] ( https://developer.microsoft.com/en-us/graph ) API.
3
2
3
+ ` python-msgraph ` is a Python wrapper of the [ Microsoft Graph] ( https://developer.microsoft.com/en-us/graph ) API.
4
4
5
5
## Installation
6
+
6
7
To install the ` python-msgraph ` library use the following command:
8
+
7
9
``` bash
8
10
python -m pip install python-msgraph
9
11
```
12
+
10
13
or, to build locally and install:
11
- ``` bash
12
- git clone
[email protected] :WMInfoTech/python-msgraph.git
&& cd python-msgraph
13
- python setup.py install
14
- ```
14
+
15
15
16
16
## Usage
17
17
18
18
### Authentication
19
+
19
20
The library currently supports connecting to the API using an SSL certificate:
21
+
20
22
``` python
21
23
from msgraph import api
22
24
@@ -31,31 +33,37 @@ api_instance = api.GraphAPI.from_certificate(authority_host_uri, tenant, resourc
31
33
32
34
** NOTE** : When a ` client_certificate ` is changed, the ` client_thumbprint ` and ` client_id ` values must also be changed
33
35
34
-
35
36
### Using the API to fetch Users
37
+
36
38
You can use the ` msgraph.user ` module to interact with ` User ` instances. ` User ` instanced can be fetched using the ` msgraph.user.User ` class:
39
+
37
40
``` python
38
41
from msgraph import user
39
42
all_users = user.User.get(api_instance)
40
43
```
41
44
42
45
To fetch a specific user, you can also include the user's ` User Principal Name ` , which is the user's email address:
46
+
43
47
``` python
44
48
johndoe_instance
= user.User.get(api_instance,
user = ' [email protected] ' )
45
49
```
46
50
47
51
### Calendars & Events
48
52
49
53
#### Fetch a User's Calendars
54
+
50
55
Now let's fetch the ` Calendar ` s of a particular user. To interact with a ` Calendar ` , ` Event ` , calendar ` Group ` , or calendar ` Category ` instance, we will use the ` msgraph.calendar ` module:
56
+
51
57
``` python
52
58
from msgraph import calendar
53
59
54
60
johndoe_calendars = calendar.Calendar.get(api_instance, user = johndoe_instance)
55
61
```
56
62
57
63
#### Fetch a User's Events from a given Calendar
64
+
58
65
Now let's fetch the ` Event ` instances from the main calendar of ` johndoe ` :
66
+
59
67
``` python
60
68
calendar_lookup = dict ()
61
69
for calendar in johndoe_calendars:
@@ -66,59 +74,76 @@ johndoe_events = calendar.Event.get(johndoe_instance, calendar=primary_calendar)
66
74
```
67
75
68
76
#### Update an Event
77
+
69
78
To update an ` Event ` , we can use the ` Event.update ` method:
79
+
70
80
``` python
71
81
johndoe_event = johndoe_events[0 ]
72
82
johndoe_event.subject = ' Important meeting'
73
83
johndoe_event.update(api_instance)
74
84
```
85
+
75
86
Now the updates made to the ` Event ` object have been saved back to the ` calendar ` of ` johndoe ` .
76
87
77
88
#### Delete an Event from a Calendar
89
+
78
90
Let's try deleting an ` Event ` on a ` Calendar ` using the ` Event.delete ` method:
91
+
79
92
``` python
80
93
johndoe_event = johndoe_events[0 ]
81
94
johndoe_event.delete(api_instance)
82
95
```
83
- After calling the ` delete ` method, the ` Event ` has been removed from the ` calendar ` of ` johndoe ` .
84
96
97
+ After calling the ` delete ` method, the ` Event ` has been removed from the ` calendar ` of ` johndoe ` .
85
98
86
99
### Sharepoint Sites & Lists
87
100
88
101
#### Search for a site
102
+
89
103
To fetch all sites matching a key phrase, use the ` msgraph.sites.Site.search ` method:
104
+
90
105
``` python
91
106
from msgraph import sites
92
-
93
107
matching_sites = sites.Site.search(api_instance, ' software' )
94
108
```
95
109
96
110
#### Fetching a specific site
111
+
97
112
Specific ` msgraph.sites.Site ` instances can be fetched using multiple methods:
98
- * ` msgraph.sites.Site.get ` method fetches sites by using the ID of the site
113
+
114
+ * ` msgraph.sites.Site.get ` method fetches sites by using the ID of the site
115
+
99
116
``` python
100
117
site_id = ' XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX'
101
118
site = sites.Site.get(api_instance, site = site_id)
102
119
```
103
- * ` msgraph.sites.Site.by_relative_url ` method to fetch by the host name and relative url of the SharePoint site
104
- ``` python
105
- host_name = ' '
106
- relative_url = ' '
107
- site = sites.Site.by_relative_url(api_instance, host_name, relative_url)
108
- ```
109
- * ` msgraph.sites.Site.by_group ` method fetches the team site of a given group
110
- ``` python
111
- group_id = ' XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX'
112
- site = sites.Site.by_group(api_instance, group = group_id)
113
- ```
120
+
121
+ * ` msgraph.sites.Site.by_relative_url ` method to fetch by the host name and relative url of the SharePoint site
122
+
123
+ ``` python
124
+ host_name = ' '
125
+ relative_url = ' '
126
+ site = sites.Site.by_relative_url(api_instance, host_name, relative_url)
127
+ ```
128
+
129
+ * ` msgraph.sites.Site.by_group ` method fetches the team site of a given group
130
+
131
+ ``` python
132
+ group_id = ' XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX'
133
+ site = sites.Site.by_group(api_instance, group = group_id)
134
+ ```
114
135
115
136
#### Traversing hierarchy of sites
137
+
116
138
SharePoint sites can have sub-sites within them. To get the subsites of a Sharepoint site, use the ` msgraph.sites.Site.subsites ` method:
139
+
117
140
``` python
141
+
118
142
subsites = site.subsites(api_instance)
119
143
```
120
144
121
145
To traverse the hierarchy of ` msgraph.sites.Site ` instances:
146
+
122
147
``` python
123
148
def breadth_first (api , root ):
124
149
queue = [root]
@@ -128,10 +153,8 @@ def breadth_first(api, root):
128
153
queue += subsites
129
154
return queue
130
155
131
-
132
156
breadth_first_hierarchy = breadth_first(api_instance, site)
133
157
134
-
135
158
def depth_first (api , root ):
136
159
queue = [root]
137
160
while queue:
@@ -140,94 +163,112 @@ def depth_first(api, root):
140
163
queue += subsites
141
164
return queue
142
165
143
-
144
166
depth_first_hierarchy = breadth_first(api_instance, site)
145
167
```
146
168
147
169
#### Fetching Lists
170
+
148
171
` msgraph.sites.SiteList ` instances can be fetched using the ` msgraph.sites.SiteList.get ` method:
172
+
149
173
``` python
150
174
site_lists = sites.SiteList.get(api_instance, site)
151
175
```
152
176
153
177
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:
178
+
154
179
``` python
155
180
list_id = ' XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX'
156
181
site_list = sites.SiteList.get(api_instance, site, list_instance = list_id)
157
182
```
158
183
159
184
** NOTE** : All ` site ` method parameters can be substituted with their IDs in the examples above. So the code below would be valid:
185
+
160
186
``` python
161
187
site_list = sites.SiteList.get(api_instance, site_id, list_instance = list_id)
162
188
```
163
189
164
-
165
190
#### Fetching ListItems for SiteLists
191
+
166
192
To fetch the ` msgraph.sites.ListItem ` instances for a ` msgraph.sites.SiteList ` , use the ` msgraph.sites.ListItem.get ` method:
193
+
167
194
``` python
168
195
list_items = sites.ListItem.get(api_instance, site, site_list)
169
196
```
170
197
171
198
#### Fetching previous versions of ListItems
199
+
172
200
` msgraph.sites.ListItem ` instances can be updated in Microsoft Graph. To fetch the previous versions, use the ` msgraph.sites.ListItem.versions ` method:
201
+
173
202
``` python
174
203
for item in list_items:
175
204
previous_versions = item.versions(api_instance, site, site_list)
176
205
```
177
206
178
207
** NOTE** : All ` site ` and ` site_list ` method parameters can be substituted with their IDs. So the code below would be valid:
208
+
179
209
``` python
180
210
list_items = sites.ListItem.get(api_instance, site_id, list_id)
181
211
for item in list_items:
182
212
previous_versions = item.versions(api_instance, site_id, list_id)
183
213
```
184
214
185
215
#### Creating a ListItem
216
+
186
217
To create a new ` msgraph.sites.ListItem ` use the ` msgraph.sites.ListItem.create ` method:
218
+
187
219
``` python
188
220
new_list_item_fields = dict (Title = ' Programmer' )
189
221
new_list_item = sites.ListItem.create(api_instance, site, list , new_list_item_fields)
190
222
```
191
223
192
224
#### Updating a ListItem
225
+
193
226
To update the properties of a ` msgraph.sites.ListItem ` instance, use the ` msgraph.sites.ListItem.update ` method:
227
+
194
228
``` python
195
229
for index, item in enumerate (list_items):
196
230
item.name = ' %s #%i ' % (item.name, index)
197
231
item.update(api_instance, site, site_list)
198
232
```
199
233
200
234
To update the fields of a ` msgraph.sites.ListItem ` instance, use the ` msgraph.sites.ListItem.update_fields ` method:
235
+
201
236
``` python
202
237
for index, item in enumerate (list_items):
203
238
item[' Title' ] = ' Assistant Executive ' + item[' Title' ]
204
239
item.update_fields(api_instance, site, site_list)
205
240
```
206
241
207
242
or alternatively:
243
+
208
244
``` python
209
245
for index, item in enumerate (list_items):
210
246
fields = dict (Title = ' Assistant Executive ' + item[' Title' ])
211
247
item.update_fields(api_instance, site, site_list, fields = fields)
212
248
```
213
249
214
250
** NOTE** : All ` site ` and ` site_list ` method parameters can be substituted with their IDs. So the code below would be valid:
251
+
215
252
``` python
216
253
for item in list_items:
217
254
item[' Title' ] = ' Assistant Executive ' + item[' Title' ]
218
255
item.update_fields(api_instance, site_id, list_id)
219
256
```
220
257
221
258
#### Deleting a ListItem
259
+
222
260
To delete an existing ` msgraph.sites.ListItem ` instance, use the ` msgraph.sites.ListItem.delete ` method:
261
+
223
262
``` python
224
263
new_list_item.delete(api_instance, site, site_list)
225
264
```
226
265
227
266
## Logging
267
+
228
268
The following modules have their own loggers:
229
- * ` msgraph.api ` - Used for logging error messages from the ` API ` and logging raw ` HTTP ` response content
230
- * ` msgraph.calendar ` - Used for logging the creation/update/deletes of ` msgraph.calendar.Calendar ` /` msgraph.calendar.Event ` /` msgraph.calendar.msgraph.calendar.Group ` /` msgraph.calendar.Category ` instances
269
+
270
+ * ` msgraph.api ` - Used for logging error messages from the ` API ` and logging raw ` HTTP ` response content
271
+ * ` msgraph.calendar ` - Used for logging the creation/update/deletes of ` msgraph.calendar.Calendar ` /` msgraph.calendar.Event ` /` msgraph.calendar.msgraph.calendar.Group ` /` msgraph.calendar.Category ` instances
231
272
* ` msgraph.group ` - Used for logging the creation/update/deletes of ` msgraph.group.Group ` instances
232
273
* ` msgraph.site ` - Used for logging the creation/update/deletes of ` msgraph.sites.Site ` instances, ` msgraph.sites.SiteList ` instances, and ` msgraph.sites.ListItem ` instances
233
274
* ` msgraph.user ` - Used for logging the creation/update/deletes of ` msgraph.user.User ` instances
0 commit comments