Microsoft 365 & Microsoft Graph Library for Python
MIT License
Bot releases are visible (Hide)
Published by vgrem 6 months ago
DriveItem.get_files
and DriveItem.get_folders
methods to address retrieving all the items (if collection exceeds the default page size)Published by vgrem 6 months ago
client = GraphClient.with_token_interactive(
tenant_name_or_id, app_client_id, admin_principal_name
)
resource = client.service_principals.get_by_name("Microsoft Graph")
app = client.applications.get_by_app_id(app_client_id)
resource.grant_application(app, "MailboxSettings.Read").execute_query()
client = GraphClient.with_token_interactive(
tenant_name_or_id, app_client_id, admin_principal_name
)
resource = client.service_principals.get_by_name("Microsoft Graph")
app_role = "User.Read.All"
user = client.users.get_by_principal_name(test_user_principal_name)
resource.grant_delegated(app_client_id, user, app_role).execute_query()
Published by vgrem 7 months ago
ClientContext.with_client_certificate
methodExample:
cert_credentials = {
"tenant": tenant_name,
"client_id": client_id,
"thumbprint": cert_thumbprint,
"cert_path": "selfsignkeyenc.pem",
"passphrase": "Password",
}
ctx = ClientContext(site_url).with_client_certificate(**cert_credentials)
current_web = ctx.web.get().execute_query()
Published by vgrem 8 months ago
Example: create a folder with a color
ctx = ClientContext(site_url).with_credentials(user_credentials)
root_folder = ctx.web.default_document_library().root_folder
folder = root_folder.folders.add(
"archive", color_hex=FolderColors.DarkGreen
).execute_query()
Published by vgrem 9 months ago
Example: get Range
client = GraphClient.with_username_and_password(
tenant_name, client_id, username, password
)
drive_item = client.me.drive.root.get_by_path("Financial Report.xlsx")
worksheets = drive_item.workbook.worksheets.get().execute_query()
if len(worksheets) == 0:
sys.exit("No worksheets found")
worksheet_range = worksheets["Sheet1"].range(address="A1:B3").execute_query()
Published by vgrem 10 months ago
Published by vgrem 11 months ago
file_name
parameter in File.copyto
and File.copyto_using_path
methodsparent_folder
and parent_collection
empty when retrieving file using ctx.web.get_file_by_server_relative_url
Published by vgrem 12 months ago
Published by vgrem about 1 year ago
Published by vgrem about 1 year ago
Folder.copy_to_using_path
methodmypy
and pyright
type checkers) by @kellerzaFile.download_session
method fixGraphClient.with_client_secret
and GraphClient.with_username_and_password
methods to initialize the client, refer below examplesGraphClient
client using user namename and password flow:from office365.graph_client import GraphClient
client = GraphClient.with_username_and_password(
"contoso.onmicrosoft.com", client_id, username, password
)
GraphClient
with client secret:from office365.graph_client import GraphClient
client = GraphClient.with_client_secret("contoso.onmicrosoft.com", client_id, client_secret)
Published by vgrem about 1 year ago
FieldCollection.add_dependent_lookup_field
by @pclasen-ebWeb.get_file_by_server_relative_path
methodPublished by vgrem about 1 year ago
ClientContext.with_interactive
method introducedClientContext
clienttenant = "contoso.onmicrosoft.com"
ctx = ClientContext(site_url).with_interactive(tenant, client_id)
me = ctx.web.current_user.get().execute_query()
print(me.login_name)
Published by vgrem over 1 year ago
When addressing a file or folder, in addition to server relative url format /sites/mysite/folder/filename.ext
, it is supported to specify url in site or web relative format now: folder/filename.ext
, for example:
ctx = ClientContext(site_url).with_credentials(credentials)
file_url = 'Shared Documents/Report.xlsx'
file = ctx.web.get_file_by_server_relative_url(file_url).get().execute_query()
Use ensure_created
method which polls for team status to determine whether it has been created:
client = GraphClient(acquire_token)
new_team = client.teams.create(team_name).ensure_created().execute_query()
Published by vgrem over 1 year ago
ClientResult
(return type) between calls when passing to execute_query
method by @vgremPublished by vgrem over 1 year ago
For methods from SharePoint API search
namespace those return type contains value pf SimpleDataTable
type, cell values getting addressed like this:
for row in results.Table.Rows:
print(row.Cells["Path"]) # prints cell value for `Path` property
Here is an complete example
ctx = ClientContext(site_url).with_credentials(user_credentials)
result = ctx.search.query("contentclass:STS_Site").execute_query()
results = result.value.PrimaryQueryResult.RelevantResults
for row in results.Table.Rows:
print(row.Cells["Path"]) # prints site url
Published by vgrem almost 2 years ago
Create a new table example
from office365.graph_client import GraphClient
from office365.onedrive.workbooks.tables.rows.row import WorkbookTableRow
file_name = "Financial Sample.xlsx"
client = GraphClient(acquire_token)
workbook = client.me.drive.root.get_by_path(file_name).workbook
table = workbook.tables.add("A1:C10", True).execute_query()
List rows example
from office365.graph_client import GraphClient
from office365.onedrive.workbooks.tables.rows.row import WorkbookTableRow
file_name = "Financial Sample.xlsx"
client = GraphClient(acquire_token)
workbook = client.me.drive.root.get_by_path(file_name).workbook
table = workbook.worksheets["Sheet1"].tables["financials"].get().execute_query()
print(table.name)
# read table content
rows = table.rows.get().execute_query()
for r in rows: # type: WorkbookTableRow
print(r.values)
Published by vgrem almost 2 years ago
ClientRuntimeContext.execute_query_retry
method by @jneuendorfwith_client_certificate
methodcert_path = 'selfsigncert.pem'
with open(cert_path, 'r') as f:
private_key = open(cert_path).read()
cert_credentials = {
'tenant': test_tenant,
'client_id': '--client id--',
'thumbprint': '--thumbprint--',
'private_key': private_key
}
ctx = ClientContext(test_team_site_url).with_client_certificate(**cert_credentials)
field_value = ImageFieldValue(image_url)
list_item.set_property(field_name, field_value).update().execute_query()
Published by vgrem about 2 years ago
ClientObjectCollection.get_all
method to retrieve all the items in a collection, regardless of the size, for example:
def print_progress(items):
"""
:type items: office365.sharepoint.listitems.collection.ListItemCollection
"""
print("Items read: {0}".format(len(items)))
page_size = 500
ctx = ClientContext(site_url).with_credentials(client_credentials)
large_list = ctx.web.lists.get_by_title(list_title)
all_items = target_list.items.get_all(page_size, print_progress).execute_query()
ClientObjectCollection.paged
methodpage_size = 500
ctx = ClientContext(site_url).with_credentials(client_credentials)
large_list = ctx.web.lists.get_by_title(list_title)
paged_items = large_list.items.paged(page_size, page_loaded=print_progress).get().execute_query()
for index, item in enumerate(paged_items): # type: int, ListItem
print("{0}: {1}".format(index, item.id))
Folder.copy_to_using_path
methodPublished by vgrem over 2 years ago
QueryOptions.build
method, kudos to @jjlonemanSharing
namespace in SharePoint APISite Designs and Site Scripts
in SharePoint APIExample: Share an anonymous access link with view permissions to a file
ctx = ClientContext(site_url).with_credentials(credentials)
file = ctx.web.get_file_by_server_relative_url(file_url)
result = target_file.share_link(SharingLinkKind.AnonymousView).execute_query()
Example: create a new site script that apply a custom theme
ctx = ClientContext(site_url).with_credentials(credentials)
site_script = {
"$schema": "schema.json",
"actions": [
{
"verb": "applyTheme",
"themeName": "Contoso Theme"
}
],
"bindata": {},
"version": 1
}
result = SiteScriptUtility.create_site_script(ctx, "Contoso theme script", "", site_script).execute_query()