Skip to content

Commit 4489512

Browse files
committed
#764 fix, SharePoint examples updates, typings enhancements
1 parent 5936441 commit 4489512

Some content is hidden

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

62 files changed

+493
-296
lines changed

examples/directory/groups/create_m365.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
grp_name = create_unique_name("Group")
1414
client = GraphClient(acquire_token_by_username_password)
15-
group = client.groups.create_m365_group(grp_name).execute_query()
15+
group = client.groups.create_m365(grp_name).execute_query()
1616

1717
# clean up resources
1818
group.delete_object(True).execute_query()

examples/directory/groups/list.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88
from tests.graph_case import acquire_token_by_username_password
99

1010
client = GraphClient(acquire_token_by_username_password)
11-
groups = client.groups.get().top(10).execute_query()
11+
groups = client.groups.get().top(100).execute_query()
1212
for grp in groups:
1313
print(grp)

examples/outlook/messages/download.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
Get MIME content of a message
2+
Downloads MIME representation of a message
33
44
https://learn.microsoft.com/en-us/graph/outlook-get-mime-message
55
Requires Mail.ReadWrite permission
@@ -12,11 +12,11 @@
1212
from tests.graph_case import acquire_token_by_username_password
1313

1414
client = GraphClient(acquire_token_by_username_password)
15-
messages = client.me.messages.select(["id", "subject"]).top(2).get().execute_query()
15+
messages = client.me.messages.select(["id", "subject"]).top(1).get().execute_query()
1616
with tempfile.TemporaryDirectory() as local_path:
1717
for message in messages:
18-
with open(os.path.join(local_path, message.id + ".eml"), "wb") as local_file:
19-
message.download(
20-
local_file
21-
).execute_query() # download MIME representation of a message
18+
with open(
19+
os.path.join(local_path, message.subject + ".eml"), "wb"
20+
) as local_file:
21+
message.download(local_file).execute_query()
2222
print("Message downloaded into {0}".format(local_file.name))

examples/planner/create_plan.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"""
2+
Creates a new plannerPlan
3+
https://learn.microsoft.com/en-us/graph/api/planner-post-plans?view=graph-rest-1.0
4+
"""
5+
6+
from office365.graph_client import GraphClient
7+
from tests import test_client_id, test_password, test_tenant, test_username
8+
9+
client = GraphClient.with_username_and_password(
10+
test_tenant, test_client_id, test_username, test_password
11+
)
12+
group = client.groups.get_by_name("My Sample Team")
13+
plan = client.planner.plans.add("My Plan", group).execute_query()
14+
print(plan)

examples/planner/create_task.py

+11-17
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,16 @@
1+
"""
2+
Create a new plannerTask.
3+
https://learn.microsoft.com/en-us/graph/api/planner-post-tasks?view=graph-rest-1.0
4+
"""
5+
import sys
6+
17
from office365.graph_client import GraphClient
2-
from office365.planner.plans.plan import PlannerPlan
38
from tests.graph_case import acquire_token_by_username_password
49

5-
6-
def ensure_plan(planner, name):
7-
"""
8-
:type planner: office365.planner.user.PlannerUser
9-
:type name: str
10-
:rtype: PlannerPlan
11-
"""
12-
plans = planner.plans.get().filter("title eq '{0}'".format(name)).execute_query()
13-
if len(plans) > 0:
14-
return plans[0]
15-
else:
16-
return planner.plans.add(title=name).execute_query()
17-
18-
1910
client = GraphClient(acquire_token_by_username_password)
20-
plan = ensure_plan(client.me.planner, "My plan")
21-
task = client.planner.tasks.add(title="New task", planId=plan.id).execute_query()
11+
group = client.groups.get_by_name("My Sample Team").get().execute_query()
12+
plans = group.planner.plans.get().execute_query()
13+
if len(plans) == 0:
14+
sys.exit("No plans were found")
15+
task = plans[0].tasks.add(title="New task").execute_query()
2216
print("Task {0} has been created".format(task.title))

examples/sharepoint/__init__.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
from office365.sharepoint.fields.creation_information import FieldCreationInformation
22
from office365.sharepoint.fields.type import FieldType
33
from office365.sharepoint.lists.creation_information import ListCreationInformation
4+
from office365.sharepoint.lists.list import List
45
from office365.sharepoint.lists.template_type import ListTemplateType
6+
from office365.sharepoint.webs.web import Web
57
from tests import create_unique_name
68

79

@@ -17,9 +19,9 @@ def upload_sample_file(context, path):
1719

1820

1921
def create_sample_tasks_list(web):
20-
"""
21-
:type web: office365.sharepoint.webs.web.Web
22-
"""
22+
# type: (Web) -> List
23+
list_title = "Company Tasks"
24+
2325
list_title = create_unique_name("Tasks N")
2426
list_create_info = ListCreationInformation(
2527
list_title, None, ListTemplateType.TasksWithTimelineAndHierarchy
@@ -29,3 +31,7 @@ def create_sample_tasks_list(web):
2931
field_info = FieldCreationInformation("Manager", FieldType.User)
3032
return_type.fields.add(field_info).execute_query()
3133
return return_type
34+
35+
36+
def configure():
37+
pass
+6-14
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,14 @@
11
"""
2-
Demonstrates how to copy a folder within a site
2+
Demonstrates how to copy a file within a site
33
"""
44
from office365.sharepoint.client_context import ClientContext
55
from tests import test_team_site_url, test_user_credentials
66

77
ctx = ClientContext(test_team_site_url).with_credentials(test_user_credentials)
88

9-
# uploads a temporary folder first in a Documents library
10-
path = "../../data/report.csv"
11-
file_from = (
12-
ctx.web.default_document_library().root_folder.files.upload(path).execute_query()
9+
file_from = ctx.web.get_file_by_server_relative_url(
10+
"Shared Documents/Financial Sample.xlsx"
1311
)
14-
15-
# copies the file with a new name into folder
16-
destination_url = "Shared Documents/archive/2002/01"
17-
file_to = file_from.copyto_using_path(destination_url, True).execute_query()
18-
print("Folder has been copied into '{0}'".format(file_to.server_relative_path))
19-
20-
# clean up
21-
file_from.delete_object().execute_query()
22-
file_to.delete_object().execute_query()
12+
folder_to = ctx.web.get_folder_by_server_relative_url("Shared Documents/archive")
13+
file_to = file_from.copyto(folder_to, True).execute_query()
14+
print("File has been copied into '{0}'".format(file_to))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"""
2+
Demonstrates how to copy a file within a site
3+
"""
4+
from office365.sharepoint.client_context import ClientContext
5+
from tests import test_team_site_url, test_user_credentials
6+
7+
ctx = ClientContext(test_team_site_url).with_credentials(test_user_credentials)
8+
9+
file_from = ctx.web.get_file_by_server_relative_path(
10+
"Shared Documents/Financial Sample.xlsx"
11+
)
12+
13+
folder_to = ctx.web.get_folder_by_server_relative_path("Shared Documents/archive")
14+
file_to = file_from.copyto_using_path(folder_to, True).execute_query()
15+
print("File has been copied into '{0}'".format(file_to.server_relative_path))

examples/sharepoint/files/create_anonymous_link.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from tests import test_client_credentials, test_team_site_url
99

1010
client = ClientContext(test_team_site_url).with_credentials(test_client_credentials)
11-
file_url = "Shared Documents/report #123.csv"
11+
file_url = "Shared Documents/Financial Sample.xlsx"
1212
file = client.web.get_file_by_server_relative_path(file_url)
1313
expires = datetime.datetime.now() + timedelta(minutes=120)
1414
result = file.create_anonymous_link_with_expiration(expires).execute_query()

examples/sharepoint/files/download_large.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66

77

88
def print_download_progress(offset):
9+
# type: (int) -> None
910
print("Downloaded '{0}' bytes...".format(offset))
1011

1112

1213
ctx = ClientContext(test_team_site_url).with_credentials(test_client_credentials)
13-
file_url = "Shared Documents/Archive/big_buck_bunny.mp4"
14+
file_url = "Shared Documents/big_buck_bunny.mp4"
1415
source_file = ctx.web.get_file_by_server_relative_path(file_url)
1516
local_file_name = os.path.join(tempfile.mkdtemp(), os.path.basename(file_url))
1617
with open(local_file_name, "wb") as local_file:
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,19 @@
1+
"""
2+
Gets the files from the folder.
3+
If 'recursive' flag set to True, it traverses all sub folders
4+
"""
15
from office365.sharepoint.client_context import ClientContext
26
from office365.sharepoint.files.file import File
3-
from office365.sharepoint.folders.folder import Folder
47
from tests import test_team_site_url, test_user_credentials
58

6-
ctx = ClientContext(test_team_site_url).with_credentials(test_user_credentials)
7-
8-
9-
def enum_folder(parent_folder, fn):
10-
"""
11-
:type parent_folder: Folder
12-
:type fn: (File)-> None
13-
"""
14-
parent_folder.expand(["Files", "Folders"]).get().execute_query()
15-
for file in parent_folder.files: # type: File
16-
fn(file)
17-
for folder in parent_folder.folders: # type: Folder
18-
enum_folder(folder, fn)
19-
209

2110
def print_file(f):
2211
# type: (File) -> None
2312
print(f.serverRelativeUrl)
2413

2514

15+
ctx = ClientContext(test_team_site_url).with_credentials(test_user_credentials)
2616
target_folder_url = "Shared Documents"
2717
root_folder = ctx.web.get_folder_by_server_relative_path(target_folder_url)
28-
# enum_folder(root_folder, print_file)
29-
3018
files = root_folder.get_files(True).execute_query()
3119
[print_file(f) for f in files]

examples/sharepoint/folders/list_folders.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""
2-
Demonstrates how to enumerate a folder
2+
Demonstrates how to retrieve folders
3+
If 'recursive' flag set to True, it traverses all sub folders
34
"""
45

56
from office365.sharepoint.client_context import ClientContext
+15-19
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,23 @@
1-
from examples.sharepoint import create_sample_tasks_list
1+
"""
2+
Creates a list item in a List
3+
"""
4+
25
from office365.sharepoint.client_context import ClientContext
3-
from office365.sharepoint.fields.multi_user_value import FieldMultiUserValue
46
from office365.sharepoint.fields.user_value import FieldUserValue
5-
from tests import test_client_credentials, test_site_url, test_user_principal_name
6-
7-
ctx = ClientContext(test_site_url).with_credentials(test_client_credentials)
8-
tasks_list = create_sample_tasks_list(ctx.web)
7+
from tests import (
8+
test_client_credentials,
9+
test_team_site_url,
10+
test_user_principal_name,
11+
)
912

10-
user = ctx.web.ensure_user(test_user_principal_name)
11-
multi_user_value = FieldMultiUserValue()
12-
multi_user_value.add(FieldUserValue.from_user(user))
13+
ctx = ClientContext(test_team_site_url).with_credentials(test_client_credentials)
14+
tasks_list = ctx.web.lists.get_by_title("Company Tasks")
15+
manager = ctx.web.site_users.get_by_principal_name(test_user_principal_name)
1316

14-
item_to_create = tasks_list.add_item(
17+
item = tasks_list.add_item(
1518
{
1619
"Title": "New Task",
17-
"AssignedTo": multi_user_value,
18-
"Manager": FieldUserValue.from_user(user),
20+
# "Manager": FieldUserValue.from_user(manager),
1921
}
2022
).execute_query()
21-
22-
multi_user_value_alt = FieldMultiUserValue()
23-
multi_user_value_alt.add(FieldUserValue(user.id))
24-
25-
item_to_create_alt = tasks_list.add_item(
26-
{"Title": "New Task 2", "AssignedTo": multi_user_value_alt}
27-
).execute_query()
23+
print("Item has been created")
+4-11
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,14 @@
11
"""
2-
Demonstrates how to create multiple list items via batch request
2+
Demonstrates how to create multiple list items
33
"""
44
from office365.sharepoint.client_context import ClientContext
5-
from office365.sharepoint.lists.template_type import ListTemplateType
65
from tests import create_unique_name, test_client_credentials, test_team_site_url
76

87
ctx = ClientContext(test_team_site_url).with_credentials(test_client_credentials)
9-
list_tasks = ctx.web.add_list(
10-
create_unique_name("Tasks"), ListTemplateType.Tasks
11-
).execute_query()
8+
tasks_list = ctx.web.lists.get_by_title("Company Tasks")
129

13-
num_of_items = 2
10+
num_of_items = 10
1411
item_props = {"Title": create_unique_name("Task")}
15-
task_items = [list_tasks.add_item(item_props) for idx in range(0, num_of_items)]
12+
task_items = [tasks_list.add_item(item_props) for idx in range(0, num_of_items)]
1613
ctx.execute_batch()
1714
print("{0} task items created".format(len(task_items)))
18-
19-
20-
print("Cleaning up temporary resources...")
21-
list_tasks.delete_object().execute_query()
+6-24
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,15 @@
11
"""
2-
Demonstrates how to delete a list item
2+
Demonstrates how to delete a List Item from a List
33
"""
44
from office365.sharepoint.client_context import ClientContext
5-
from office365.sharepoint.lists.template_type import ListTemplateType
6-
from tests import create_unique_name, test_client_credentials, test_team_site_url
7-
8-
9-
def create_tasks_list(client):
10-
"""
11-
:type client: ClientContext
12-
"""
13-
list_title = create_unique_name("Tasks")
14-
target_list = client.web.add_list(list_title, ListTemplateType.Tasks)
15-
target_list.add_item({"Title": "Task1"})
16-
target_list.add_item({"Title": "Task2"})
17-
return target_list.expand(["items"]).get()
18-
5+
from tests import test_client_credentials, test_team_site_url
196

207
ctx = ClientContext(test_team_site_url).with_credentials(test_client_credentials)
21-
tasks_list = create_tasks_list(ctx).execute_query()
8+
tasks_list = ctx.web.lists.get_by_title("Company Tasks")
9+
items = tasks_list.items.get().execute_query()
2210

2311
print("Option 1: remove a list item (with an option to restore from a recycle bin)...")
24-
item_id = tasks_list.items[1].id
25-
tasks_list.get_item_by_id(item_id).recycle().execute_query()
12+
items[0].recycle().execute_query()
2613

2714
print("Option 2: Permanently remove a list item...")
28-
item_id = tasks_list.items[2].id
29-
tasks_list.get_item_by_id(item_id).delete_object().execute_query()
30-
31-
32-
print("Cleaning up...")
33-
tasks_list.delete_object().execute_query()
15+
items[1].delete_object().execute_query()
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1+
"""
2+
Demonstrates how to delete multiple list items
3+
"""
14
from office365.sharepoint.client_context import ClientContext
2-
from office365.sharepoint.listitems.listitem import ListItem
35
from tests import test_client_credentials, test_team_site_url
46

57
ctx = ClientContext(test_team_site_url).with_credentials(test_client_credentials)
6-
items_count = 100
8+
items_count = 10
79

810
# 1. Load existing list items
9-
list_tasks = ctx.web.lists.get_by_title("Tasks")
10-
items = list_tasks.items.get().top(items_count).execute_query()
11+
tasks_list = ctx.web.lists.get_by_title("Company Tasks")
12+
items = tasks_list.items.get().top(items_count).execute_query()
1113

1214
# 2. Delete list items (via batch mode)
13-
for item in items: # type: ListItem
14-
item.delete_object()
15+
[item.delete_object() for item in items]
1516
ctx.execute_batch()
1617
print("{0} items have been deleted".format(items_count))

0 commit comments

Comments
 (0)