Skip to content

Commit

Permalink
refactorings & examples updates
Browse files Browse the repository at this point in the history
  • Loading branch information
vgrem committed Feb 23, 2024
1 parent 96c31ae commit 2c0c2ec
Show file tree
Hide file tree
Showing 17 changed files with 202 additions and 118 deletions.
13 changes: 10 additions & 3 deletions examples/onedrive/shares/read_workbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@
Get workbook
"""
from office365.graph_client import GraphClient
from tests import test_team_site_url
from tests.graph_case import acquire_token_by_username_password
from tests import (
test_client_id,
test_password,
test_team_site_url,
test_tenant,
test_username,
)

file_abs_url = "{0}/Shared Documents/Financial Sample.xlsx".format(test_team_site_url)

client = GraphClient(acquire_token_by_username_password)
client = GraphClient.with_username_and_password(
test_tenant, test_client_id, test_username, test_password
)
drive_item = client.shares.by_url(file_abs_url).drive_item.get().execute_query()
worksheets = drive_item.workbook.worksheets.get().execute_query()
for ws in worksheets:
Expand Down
15 changes: 0 additions & 15 deletions examples/onedrive/sites/list_permission.py

This file was deleted.

7 changes: 3 additions & 4 deletions examples/onedrive/sites/list_permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
"""

from office365.graph_client import GraphClient
from tests import test_team_site_url
from tests.graph_case import acquire_token_by_client_credentials
from tests import test_client_id, test_client_secret, test_team_site_url, test_tenant

client = GraphClient(acquire_token_by_client_credentials)
client = GraphClient.with_client_secret(test_tenant, test_client_id, test_client_secret)
permissions = (
client.sites.get_by_url(test_team_site_url).permissions.get().execute_query()
)
for perm in permissions:
print(perm.granted_to)
print("Roles: {0}, Granted to: {1}".format(perm.roles, perm.granted_to))
4 changes: 2 additions & 2 deletions examples/onedrive/sites/list_sites.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
"""

from office365.graph_client import GraphClient
from tests.graph_case import acquire_token_by_client_credentials
from tests import test_client_id, test_client_secret, test_tenant

client = GraphClient(acquire_token_by_client_credentials)
client = GraphClient.with_client_secret(test_tenant, test_client_id, test_client_secret)
sites = client.sites.paged(100).get().execute_query()
for site in sites:
print("Site url: {0}".format(site.web_url))
14 changes: 9 additions & 5 deletions examples/onedrive/sites/revoke_permission.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@
"""

from office365.graph_client import GraphClient
from tests import test_client_credentials, test_team_site_url
from tests.graph_case import acquire_token_by_client_credentials
from tests import (
test_client_credentials,
test_client_id,
test_client_secret,
test_team_site_url,
test_tenant,
)

client = GraphClient(acquire_token_by_client_credentials)
client = GraphClient.with_client_secret(test_tenant, test_client_id, test_client_secret)

app = client.applications.get_by_app_id(test_client_credentials.clientId)
site = client.sites.get_by_url(test_team_site_url)
site.permissions.delete(["write"], app).execute_query()
# site.permissions.delete_all().execute_query()
site.permissions.delete_all().execute_query()
18 changes: 18 additions & 0 deletions examples/sharepoint/folders/share.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"""
Shares a folder
"""
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.sharing.external_site_option import ExternalSharingSiteOption
from tests import (
test_team_site_url,
test_user_credentials,
test_user_principal_name,
)

ctx = ClientContext(test_team_site_url).with_credentials(test_user_credentials)
folder_url = "Shared Documents/Archive"
folder = ctx.web.get_folder_by_server_relative_path(folder_url)
result = folder.list_item_all_fields.share(
test_user_principal_name, ExternalSharingSiteOption.View
).execute_query()
print(result.url)
26 changes: 15 additions & 11 deletions examples/sharepoint/taxonomy/set_field_value.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,33 @@
from tests import create_unique_name, test_client_credentials, test_team_site_url

ctx = ClientContext(test_team_site_url).with_credentials(test_client_credentials)
custom_list = ctx.web.lists.get_by_title("Requests").get().execute_query()

print("Creating a custom list...")
custom_list = ctx.web.add_list(create_unique_name("Custom List")).execute_query()

print("Adding a taxonomy field into list '{0}'...".format(custom_list.title))
term_set_id = "3b712032-95c4-4bb5-952d-f85ae9288f99"
tax_field_name = create_unique_name("Country")
mult_tax_field_name = create_unique_name("Countries")

print("1. Adding a taxonomy field into list '{0}'...".format(custom_list.title))
tax_field = custom_list.fields.create_taxonomy_field(
"Country", term_set_id
tax_field_name, term_set_id
).execute_query()
multi_tax_field = custom_list.fields.create_taxonomy_field(
"Countries", term_set_id, allow_multiple_values=True
mult_tax_field_name, term_set_id, allow_multiple_values=True
).execute_query()

print("Creating a list item and setting a taxonomy field value ...")
print("2. Creating a list item and setting a taxonomy field value ...")
item = custom_list.add_item(
{
"Title": "New item",
"Country": TaxonomyFieldValue("Sweden", "f9a6dae9-633c-474b-b35e-b235cf2b9e73"),
"Countries": TaxonomyFieldValueCollection(
tax_field_name: TaxonomyFieldValue(
"Sweden", "f9a6dae9-633c-474b-b35e-b235cf2b9e73"
),
mult_tax_field_name: TaxonomyFieldValueCollection(
[TaxonomyFieldValue("Sweden", "f9a6dae9-633c-474b-b35e-b235cf2b9e73")]
),
}
).execute_query()

print("Cleaning up temporary resources...")
custom_list.delete_object().execute_query()
print("3. Deleting tax fields ...")
tax_field.delete_object().execute_query()
multi_tax_field.delete_object().execute_query()
Loading

0 comments on commit 2c0c2ec

Please sign in to comment.