Skip to content

Commit

Permalink
OneDrive sitepages namespace support ^ examples update
Browse files Browse the repository at this point in the history
  • Loading branch information
vgrem committed Mar 24, 2024
1 parent 9fd531d commit 2ed6c44
Show file tree
Hide file tree
Showing 23 changed files with 636 additions and 156 deletions.
8 changes: 5 additions & 3 deletions examples/directory/applications/list_applications.py
@@ -1,12 +1,14 @@
"""
Get the list of applications in this organization
https://learn.microsoft.com/en-us/graph/api/application-list?view=graph-rest-1.0&tabs=http
https://learn.microsoft.com/en-us/graph/api/application-list?view=graph-rest-1.0
"""

from office365.graph_client import GraphClient
from tests.graph_case import acquire_token_by_username_password
from tests import test_client_id, test_password, test_tenant, test_username

client = GraphClient(acquire_token_by_username_password)
client = GraphClient.with_username_and_password(
test_tenant, test_client_id, test_username, test_password
)
apps = client.applications.get().top(10).execute_query()
for app in apps:
print(app)
13 changes: 10 additions & 3 deletions examples/directory/groups/create_with_team.py
Expand Up @@ -5,8 +5,13 @@
"""

from office365.graph_client import GraphClient
from tests import create_unique_name
from tests.graph_case import acquire_token_by_username_password
from tests import (
create_unique_name,
test_client_id,
test_password,
test_tenant,
test_username,
)


def print_failure(retry_number, ex):
Expand All @@ -16,7 +21,9 @@ def print_failure(retry_number, ex):
print(f"{retry_number}: Team creation still in progress, waiting...")


client = GraphClient(acquire_token_by_username_password)
client = GraphClient.with_username_and_password(
test_tenant, test_client_id, test_username, test_password
)
group_name = create_unique_name("Flight")
group = client.groups.create_with_team(group_name).execute_query_retry(
max_retry=10, failure_callback=print_failure
Expand Down
8 changes: 5 additions & 3 deletions examples/directory/groups/delete_batch.py
@@ -1,13 +1,15 @@
"""
Delete groups in batch mode
https://learn.microsoft.com/en-us/graph/api/group-delete?view=graph-rest-1.0&tabs=http
https://learn.microsoft.com/en-us/graph/api/group-delete?view=graph-rest-1.0
"""

from office365.graph_client import GraphClient
from tests.graph_case import acquire_token_by_username_password
from tests import test_client_id, test_password, test_tenant, test_username

client = GraphClient(acquire_token_by_username_password)
client = GraphClient.with_username_and_password(
test_tenant, test_client_id, test_username, test_password
)

result = client.groups.get_all().execute_query()
print("Total groups count (before): {0}".format(len(result)))
Expand Down
10 changes: 6 additions & 4 deletions examples/directory/groups/delete_groups.py
Expand Up @@ -6,20 +6,22 @@
- Group.delete_object() Microsoft 365 groups are moved to a temporary container and can be restored within 30 days
- Group.delete_object(permanent_delete=True) Microsoft 365 permanently deleted
https://learn.microsoft.com/en-us/graph/api/group-delete?view=graph-rest-1.0&tabs=http
https://learn.microsoft.com/en-us/graph/api/group-delete?view=graph-rest-1.0
"""
from office365.graph_client import GraphClient
from tests.graph_case import acquire_token_by_username_password
from tests import test_client_id, test_password, test_tenant, test_username

client = GraphClient(acquire_token_by_username_password)
client = GraphClient.with_username_and_password(
test_tenant, test_client_id, test_username, test_password
)
groups = client.groups.get().top(10).execute_query()
deletedCount = 0
groups_count = len(groups)
while len(groups) > 0:
cur_grp = groups[0]
print(
"({0} of {1}) Deleting {2} group ...".format(
deletedCount + 1, groups_count, cur_grp.properties["displayName"]
deletedCount + 1, groups_count, cur_grp.display_name
)
)
cur_grp.delete_object(permanent_delete=True).execute_query()
Expand Down
13 changes: 10 additions & 3 deletions examples/directory/users/import.py
Expand Up @@ -2,8 +2,13 @@

from office365.directory.users.profile import UserProfile
from office365.graph_client import GraphClient
from tests import create_unique_name, test_tenant
from tests.graph_case import acquire_token_by_username_password
from tests import (
create_unique_name,
test_client_id,
test_password,
test_tenant,
test_username,
)


def generate_user_profile():
Expand All @@ -23,7 +28,9 @@ def generate_user_profile():
return UserProfile(**user_json)


client = GraphClient(acquire_token_by_username_password)
client = GraphClient.with_username_and_password(
test_tenant, test_client_id, test_username, test_password
)

for idx in range(0, 1):
user_profile = generate_user_profile()
Expand Down
8 changes: 5 additions & 3 deletions examples/insights/list_used.py
Expand Up @@ -5,9 +5,11 @@
"""

from office365.graph_client import GraphClient
from tests.graph_case import acquire_token_by_username_password
from tests import test_client_id, test_password, test_tenant, test_username

client = GraphClient(acquire_token_by_username_password)
client = GraphClient.with_username_and_password(
test_tenant, test_client_id, test_username, test_password
)
result = client.me.insights.used.get().execute_query()
for item in result:
print("Resource url: {0}".format(item.resource_reference))
print("Resource: {0}".format(item.resource_reference))
6 changes: 4 additions & 2 deletions examples/onedrive/excel/list_worksheets.py
Expand Up @@ -7,9 +7,11 @@
import sys

from office365.graph_client import GraphClient
from tests.graph_case import acquire_token_by_username_password
from tests import test_client_id, test_password, test_tenant, test_username

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.me.drive.root.get_by_path("Financial Sample.xlsx")
worksheets = drive_item.workbook.worksheets.get().execute_query()
if len(worksheets) == 0:
Expand Down
8 changes: 5 additions & 3 deletions examples/onedrive/files/create_sharing_link.py
Expand Up @@ -5,13 +5,15 @@
The sharing link is configured to be read-only and usable by anyone with the link.
All existing permissions are removed when sharing for the first time if retainInheritedPermissions is false.
https://learn.microsoft.com/en-us/graph/api/driveitem-createlink?view=graph-rest-1.0&tabs=http
https://learn.microsoft.com/en-us/graph/api/driveitem-createlink?view=graph-rest-1.0
"""

from office365.graph_client import GraphClient
from tests.graph_case import acquire_token_by_username_password
from tests import test_client_id, test_password, test_tenant, test_username

client = GraphClient(acquire_token_by_username_password)
client = GraphClient.with_username_and_password(
test_tenant, test_client_id, test_username, test_password
)
file_path = "Financial Sample.xlsx"
drive_item = client.me.drive.root.get_by_path(file_path)
permission = drive_item.create_link(
Expand Down
7 changes: 3 additions & 4 deletions examples/onedrive/sites/get_by_url.py
@@ -1,12 +1,11 @@
"""
Get site by url
https://learn.microsoft.com/en-us/graph/api/site-get?view=graph-rest-1.0&tabs=http
https://learn.microsoft.com/en-us/graph/api/site-get?view=graph-rest-1.0
"""
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)
site = client.sites.get_by_url(test_team_site_url).get().execute_query()
print("Site Id: {0}".format(site.id))
12 changes: 8 additions & 4 deletions examples/onedrive/sites/grant_permission.py
Expand Up @@ -5,13 +5,17 @@
"""

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_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)

print("Retrieving app...")
app = client.applications.get_by_app_id(test_client_credentials.clientId)
app = client.applications.get_by_app_id(test_client_id)

print("Granting an Application a permissions on Site...")
site = client.sites.get_by_url(test_team_site_url)
Expand Down
4 changes: 2 additions & 2 deletions examples/onedrive/sites/search.py
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.search("team").execute_query()
for site in sites:
print("Site url: {0}".format(site.web_url))
5 changes: 2 additions & 3 deletions examples/onedrive/termstore/export_term_store.py
Expand Up @@ -2,10 +2,9 @@
Demonstrates how to retrieve a flat list of all TermSet objects
"""
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)
term_sets = (
client.sites.get_by_url(test_team_site_url)
.term_store.get_all_term_sets()
Expand Down
10 changes: 7 additions & 3 deletions examples/outlook/messages/get_basic_props.py
Expand Up @@ -4,10 +4,14 @@
"""

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

client = GraphClient(acquire_token_by_client_credentials)
client = GraphClient.with_client_secret(test_tenant, test_client_id, test_client_secret)
user = client.users[test_user_principal_name]
messages = user.messages.select(["id", "subject"]).top(10).get().execute_query()
for message in messages:
Expand Down
12 changes: 8 additions & 4 deletions examples/sharepoint/sites/grant_app_access.py
Expand Up @@ -6,12 +6,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_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)
site = client.sites.get_by_url(test_team_site_url)
app = client.applications.get_by_app_id(test_client_credentials.clientId)
app = client.applications.get_by_app_id(test_client_id)
roles = ["read", "write"]

print("Granting {0} permissions for application {1}".format(roles, app))
Expand Down
14 changes: 14 additions & 0 deletions examples/sharepoint/users/add_to_web.py
@@ -0,0 +1,14 @@
"""
If the specified login name belongs to a valid user of the site, returns the User object corresponding to that user.
If the specified login name belongs to a valid user outside of the site, adds the user to the site and
returns the User object corresponding to that user.
"""
from office365.sharepoint.client_context import ClientContext
from tests import test_admin_credentials, test_team_site_url, test_user_principal_name

client = ClientContext(test_team_site_url).with_credentials(test_admin_credentials)
target_user = client.web.ensure_user(test_user_principal_name).execute_query()
print(target_user)
17 changes: 12 additions & 5 deletions generator/import_metadata.py
Expand Up @@ -3,8 +3,13 @@

from office365.graph_client import GraphClient
from office365.sharepoint.client_context import ClientContext
from tests import test_client_credentials, test_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_site_url,
test_tenant,
)


def export_to_file(path, content):
Expand All @@ -21,13 +26,13 @@ def export_to_file(path, content):
"--endpoint",
dest="endpoint",
help="Import metadata endpoint",
default="microsoftgraph",
default="sharepoint",
)
parser.add_argument(
"-p",
"--path",
dest="path",
default="./metadata/MicrosoftGraph.xml",
default="./metadata/SharePoint.xml",
help="Import metadata endpoint",
)

Expand All @@ -40,6 +45,8 @@ def export_to_file(path, content):
export_to_file(args.path, result.value)
elif args.endpoint == "microsoftgraph":
print("Importing Microsoft Graph model metadata...")
client = GraphClient(acquire_token_by_client_credentials)
client = GraphClient.with_client_secret(
test_tenant, test_client_id, test_client_secret
)
result = client.get_metadata().execute_query()
export_to_file(args.path, result.value)
4 changes: 4 additions & 0 deletions generator/metadata/MicrosoftGraph.xml
Expand Up @@ -21380,6 +21380,7 @@
<NavigationProperty Name="ownedObjects" Type="Collection(graph.directoryObject)"/>
<NavigationProperty Name="registeredDevices" Type="Collection(graph.directoryObject)"/>
<NavigationProperty Name="scopedRoleMemberOf" Type="Collection(graph.scopedRoleMembership)" ContainsTarget="true"/>
<NavigationProperty Name="sponsors" Type="Collection(graph.directoryObject)"/>
<NavigationProperty Name="transitiveMemberOf" Type="Collection(graph.directoryObject)"/>
<NavigationProperty Name="calendar" Type="graph.calendar" ContainsTarget="true"/>
<NavigationProperty Name="calendarGroups" Type="Collection(graph.calendarGroup)" ContainsTarget="true"/>
Expand Down Expand Up @@ -38239,6 +38240,7 @@
<NavigationPropertyBinding Path="ownedDevices" Target="directoryObjects"/>
<NavigationPropertyBinding Path="ownedObjects" Target="directoryObjects"/>
<NavigationPropertyBinding Path="registeredDevices" Target="directoryObjects"/>
<NavigationPropertyBinding Path="sponsors" Target="directoryObjects"/>
<NavigationPropertyBinding Path="transitiveMemberOf" Target="directoryObjects"/>
</EntitySet>
<EntitySet Name="applicationTemplates" EntityType="microsoft.graph.applicationTemplate"/>
Expand Down Expand Up @@ -38351,6 +38353,7 @@
<NavigationPropertyBinding Path="ownedDevices" Target="directoryObjects"/>
<NavigationPropertyBinding Path="ownedObjects" Target="directoryObjects"/>
<NavigationPropertyBinding Path="registeredDevices" Target="directoryObjects"/>
<NavigationPropertyBinding Path="sponsors" Target="directoryObjects"/>
<NavigationPropertyBinding Path="transitiveMemberOf" Target="directoryObjects"/>
</Singleton>
<Singleton Name="policies" Type="microsoft.graph.policyRoot"/>
Expand Down Expand Up @@ -38862,6 +38865,7 @@
<Property Name="lastModifiedBy" Type="Edm.String"/>
<Property Name="lastUpdateDateTime" Type="Edm.DateTimeOffset" Nullable="false"/>
<Property Name="redirectIncidentId" Type="Edm.String"/>
<Property Name="resolvingComment" Type="Edm.String"/>
<Property Name="severity" Type="microsoft.graph.security.alertSeverity" Nullable="false"/>
<Property Name="status" Type="microsoft.graph.security.incidentStatus" Nullable="false"/>
<Property Name="systemTags" Type="Collection(Edm.String)"/>
Expand Down

0 comments on commit 2ed6c44

Please sign in to comment.