Skip to content

Commit 310ccf2

Browse files
vvgrem@gmail.comvvgrem@gmail.com
authored andcommitted
SharePoint API: Tenant, ContentType new methods & properties, unit tests & examples minor refactorings
1 parent 2f486ed commit 310ccf2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+342
-116
lines changed

README-dev.md

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ $ pip install -r requirements-dev.txt
1010

1111
# Running tests
1212

13-
Most of the tests are end-to-end - connecting to real sites (not mocked). So one has to configure his/her office/sharepoint credentials. To do so, create a file ```.env``` like this (replace the bracketed values by your values):
13+
Most of the tests are end-to-end - operations are invoked against actual tenant (not mocked).
14+
So one has to configure his/her office/sharepoint credentials.
15+
To do so, create a file ```.env``` like this (replace the bracketed values by your values):
1416

1517
```
16-
export Office365_Python_Sdk_SecureVars='{username};{password};{client_id};{client_password}'
17-
export COMPANY={your_company}
18+
export office365_python_sdk_securevars='{username};{password};{client_id};{client_password}'
1819
```
1920

2021
This file is in .gitignore, so it will never be committed.
@@ -24,5 +25,13 @@ $ . .env # source it to export the variable
2425
$ nose2 ... # run the test(s) you need...
2526
```
2627

27-
# Configure Tenant
28+
#### Configure Tenant
2829

30+
Roles:
31+
32+
- Global reader
33+
- Groups admin
34+
- Search admin
35+
- SharePoint admin
36+
- Teams service admin
37+
- Users admin

examples/directory/clear_directory.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ def get_token_for_user():
2121
# deleted_users = client.directory.deletedUsers.get().execute_query()
2222
groups_count = len(deleted_groups)
2323

24-
for index, deleted_grp in enumerate(deleted_groups):
25-
print("({0} of {1}) Deleting {2} group ...".format(index + 1, groups_count, deleted_grp.properties['displayName']))
26-
deleted_grp.delete_object()
27-
client.execute_query()
24+
index = 0
25+
while len(deleted_groups) > 0:
26+
cur_grp = deleted_groups[0]
27+
print("({0} of {1}) Deleting {2} group ...".format(index + 1, groups_count, cur_grp.properties['displayName']))
28+
cur_grp.delete_object().execute_query()
2829
print("Group deleted.")
30+
index += 1

examples/onedrive/export_files.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ def download_files(remote_folder, local_path):
4343
client = GraphClient(get_token)
4444

4545
# load drive properties
46-
drive = client.users["jdoe@mediadev8.onmicrosoft.com"].drive
46+
target_user_name = settings.get('test_accounts')[1]
47+
drive = client.users[target_user_name].drive
4748
# download files from OneDrive
4849
with tempfile.TemporaryDirectory() as path:
4950
download_files(drive.root, path)

examples/onedrive/import_files.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ def upload_files(remote_drive, local_root_path):
3737

3838
# get target drive
3939
client = GraphClient(get_token)
40-
target_drive = client.users["jdoe@mediadev8.onmicrosoft.com"].drive
40+
user_name = settings.get('test_accounts')[1]
41+
target_drive = client.users[user_name].drive
4142
# import local files into OneDrive
4243
upload_files(target_drive, "../data")

examples/onedrive/upload_file.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ def acquire_token():
2121

2222

2323
client = GraphClient(acquire_token)
24-
target_drive = client.users["jdoe@mediadev8.onmicrosoft.com"].drive
24+
user_name = settings.get('test_accounts')[1]
25+
target_drive = client.users[user_name].drive
2526

2627
local_path = "../../tests/data/SharePoint User Guide.docx"
2728
with open(local_path, 'rb') as f:

examples/outlook/send_message.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ def get_token():
2626
"ToRecipients": [
2727
{
2828
"EmailAddress": {
29-
"Address": "vgrem@mediadev8.onmicrosoft.com"
29+
"Address": settings.get('test_accounts')[1]
3030
}
3131
}
3232
]
3333
},
3434
"SaveToSentItems": "false"
3535
}
3636

37-
login_name = "mdoe@mediadev8.onmicrosoft.com"
38-
client.users[login_name].send_mail(message_json)
37+
user_name = settings.get('test_accounts')[0]
38+
client.users[user_name].send_mail(message_json)
3939
client.execute_query()

examples/sharepoint/connect_with_azure_app.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import os
22

33
from office365.sharepoint.client_context import ClientContext
4+
from settings import settings
45

56
app_settings = {
6-
'url': 'https://mediadev8.sharepoint.com/sites/team',
7+
'url': settings.get('team_site_url'),
78
'client_id': '51d03106-4726-442c-86db-70b32fa7547f',
89
'thumbprint': "6B36FBFC86FB1C019EB6496494B9195E6D179DDB",
910
'certificate_path': '{0}/selfsigncert.pem'.format(os.path.dirname(__file__))

examples/sharepoint/lists_and_items/data_generator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def generate_contacts(context):
7676

7777

7878
if __name__ == '__main__':
79-
ctx = ClientContext.connect_with_credentials("https://mediadev8.sharepoint.com/sites/team",
79+
ctx = ClientContext(settings.get('team_site_url')).with_credentials(
8080
UserCredential(settings['user_credentials']['username'],
8181
settings['user_credentials']['password']))
8282
# generate_contacts(ctx)

examples/sharepoint/export/exportList.py renamed to examples/sharepoint/lists_and_items/export_list_items_csv.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,15 @@
77
from office365.runtime.auth.client_credential import ClientCredential
88
from office365.sharepoint.client_context import ClientContext
99

10-
"Demonstrates how to export a List data"
10+
"Demonstrates how to export a List data as csv"
1111

12-
ctx = ClientContext.connect_with_credentials("https://mediadev8.sharepoint.com/sites/team",
12+
ctx = ClientContext(settings.get('team_site_url')).with_credentials(
1313
ClientCredential(settings['client_credentials']['client_id'],
1414
settings['client_credentials']['client_secret']))
1515
# 1.retrieve list data
1616
list_title = "Contacts_Large"
1717
list_to_export = ctx.web.lists.get_by_title(list_title)
18-
list_items = list_to_export.items.top(100)
19-
ctx.load(list_items)
20-
ctx.execute_query()
18+
list_items = list_to_export.items.top(100).get().execute_query()
2119
if len(list_items) == 0:
2220
print("No data found")
2321

examples/sharepoint/lists_and_items/read_large_list.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def get_items(target_list):
4848
print("Item at index: {0}".format(items[index].properties))
4949

5050

51-
ctx = ClientContext.connect_with_credentials("https://mediadev8.sharepoint.com/sites/team",
51+
ctx = ClientContext(settings.get('team_site_url')).with_credentials(
5252
ClientCredential(settings['client_credentials']['client_id'],
5353
settings['client_credentials']['client_secret']))
5454

0 commit comments

Comments
 (0)