Skip to content

Commit

Permalink
SharePoint API: improvements for file/folder addressing when file nam…
Browse files Browse the repository at this point in the history
…e contains % and # sybmols (#346)
  • Loading branch information
[email protected] authored and [email protected] committed May 22, 2021
1 parent 859d0b9 commit 8eca1ff
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 14 deletions.
1 change: 1 addition & 0 deletions examples/data/report #123.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
123
2 changes: 1 addition & 1 deletion examples/directory/delete_groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

client = GraphClient(acquire_token_by_username_password)

groups = client.groups.get().top(100).execute_query()
groups = client.groups.get().top(500).execute_query()
deletedCount = 0
groups_count = len(groups)
while len(groups) > 0:
Expand Down
4 changes: 2 additions & 2 deletions examples/onedrive/import_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def upload_files(remote_drive, local_root_path):

settings = load_settings()
client = GraphClient(acquire_token_by_client_credentials)
user_name = settings.get('test_alt_account_name')
target_drive = client.users[user_name].drive # get target drive
test_user_principal_name_alt = settings.get('users', 'test_user2')
target_drive = client.users[test_user_principal_name_alt].drive # get target drive
# import local files into OneDrive
upload_files(target_drive, "../data")
5 changes: 3 additions & 2 deletions examples/sharepoint/files/download_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
from tests import test_team_site_url, test_client_credentials

ctx = ClientContext(test_team_site_url).with_credentials(test_client_credentials)
file_url = '/sites/team/Shared Documents/big_buck_bunny.mp4'
# file_url = '/sites/team/Shared Documents/big_buck_bunny.mp4'
file_url = "/sites/team/Shared Documents/report #123.csv"
download_path = os.path.join(tempfile.mkdtemp(), os.path.basename(file_url))
with open(download_path, "wb") as local_file:
file = ctx.web.get_file_by_server_relative_url(file_url).download(local_file).execute_query()
file = ctx.web.get_file_by_server_relative_path(file_url).download(local_file).execute_query()
print("[Ok] file has been downloaded: {0}".format(download_path))
6 changes: 3 additions & 3 deletions examples/sharepoint/files/upload_file.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import os

from office365.sharepoint.client_context import ClientContext
from tests import test_site_url, test_user_credentials
from tests import test_site_url, test_user_credentials, test_team_site_url

ctx = ClientContext(test_site_url).with_credentials(test_user_credentials)
ctx = ClientContext(test_team_site_url).with_credentials(test_user_credentials)

path = "../../../tests/data/SharePoint User Guide.docx"
path = "../../data/report #123.csv"
with open(path, 'rb') as content_file:
file_content = content_file.read()

Expand Down
6 changes: 3 additions & 3 deletions examples/sharepoint/files/upload_large_file.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import os

from office365.sharepoint.client_context import ClientContext
from tests import test_site_url, test_user_credentials
from tests import test_site_url, test_user_credentials, test_team_site_url

ctx = ClientContext(test_site_url).with_credentials(test_user_credentials)
ctx = ClientContext(test_team_site_url).with_credentials(test_user_credentials)

target_url = "/Shared Documents"
target_url = "/sites/team/Shared Documents"
target_folder = ctx.web.get_folder_by_server_relative_url(target_url)
size_chunk = 1000000
local_path = "../../../tests/data/big_buck_bunny.mp4"
Expand Down
5 changes: 3 additions & 2 deletions office365/sharepoint/files/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ def save_binary(ctx, server_relative_url, content):
:type server_relative_url: str
:type content: str
"""
url = r"{0}web/getFileByServerRelativeUrl('{1}')/\$value".format(
url = r"{0}web/getFileByServerRelativePath(DecodedUrl='{1}')/\$value".format(
ctx.service_root_url(), server_relative_url)
request = RequestOptions(url)
request.method = HttpMethod.Post
Expand All @@ -288,7 +288,8 @@ def open_binary(ctx, server_relative_url):
:type server_relative_url: str
:return Response
"""
url = r"{0}web/getfilebyserverrelativeurl('{1}')/\$value".format(ctx.service_root_url(), server_relative_url)
url = r"{0}web/getFileByServerRelativePath(DecodedUrl='{1}')/\$value".format(ctx.service_root_url(),
server_relative_url)
request = RequestOptions(url)
request.method = HttpMethod.Get
response = ctx.execute_request_direct(request)
Expand Down
24 changes: 24 additions & 0 deletions office365/sharepoint/webs/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,17 @@ def get_file_by_server_relative_url(self, url):
ResourcePathServiceOperation("getFileByServerRelativeUrl", [url], self.resource_path)
)

def get_file_by_server_relative_path(self, decoded_url):
"""Returns the file object located at the specified server-relative path.
Prefer this method over get_folder_by_server_relative_url since it supports % and # symbols in names
:type decoded_url: str
"""
return File(
self.context,
ResourcePathServiceOperation("getFileByServerRelativePath", {"DecodedUrl": decoded_url}, self.resource_path)
)

def get_folder_by_server_relative_url(self, url):
"""Returns the folder object located at the specified server-relative URL.
:type url: str
Expand All @@ -258,6 +269,19 @@ def get_folder_by_server_relative_url(self, url):
ResourcePathServiceOperation("getFolderByServerRelativeUrl", [url], self.resource_path)
)

def get_folder_by_server_relative_path(self, decoded_url):
"""Returns the folder object located at the specified server-relative URL.
Prefer this method over get_folder_by_server_relative_url since it supports % and # symbols
Details: https://docs.microsoft.com/en-us/sharepoint/dev/solution-guidance/supporting-and-in-file-and-folder-with-the-resourcepath-api
:type decoded_url: str
"""
params = {"DecodedUrl": decoded_url}
return File(
self.context,
ResourcePathServiceOperation("getFolderByServerRelativePath", params, self.resource_path)
)

def ensure_folder_path(self, path):
"""
Ensures a nested folder hierarchy exist
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

setup(
name="Office365-REST-Python-Client",
version="2.3.3",
version="2.3.4",
author="Vadim Gremyachev",
author_email="[email protected]",
maintainer="Konrad Gądek, Domenico Di Nicola",
Expand Down

0 comments on commit 8eca1ff

Please sign in to comment.