Skip to content

Commit 8eca1ff

Browse files
SharePoint API: improvements for file/folder addressing when file name contains % and # sybmols (#346)
1 parent 859d0b9 commit 8eca1ff

File tree

9 files changed

+41
-14
lines changed

9 files changed

+41
-14
lines changed

examples/data/report #123.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
123

examples/directory/delete_groups.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
client = GraphClient(acquire_token_by_username_password)
55

6-
groups = client.groups.get().top(100).execute_query()
6+
groups = client.groups.get().top(500).execute_query()
77
deletedCount = 0
88
groups_count = len(groups)
99
while len(groups) > 0:

examples/onedrive/import_files.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def upload_files(remote_drive, local_root_path):
2424

2525
settings = load_settings()
2626
client = GraphClient(acquire_token_by_client_credentials)
27-
user_name = settings.get('test_alt_account_name')
28-
target_drive = client.users[user_name].drive # get target drive
27+
test_user_principal_name_alt = settings.get('users', 'test_user2')
28+
target_drive = client.users[test_user_principal_name_alt].drive # get target drive
2929
# import local files into OneDrive
3030
upload_files(target_drive, "../data")

examples/sharepoint/files/download_file.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
from tests import test_team_site_url, test_client_credentials
66

77
ctx = ClientContext(test_team_site_url).with_credentials(test_client_credentials)
8-
file_url = '/sites/team/Shared Documents/big_buck_bunny.mp4'
8+
# file_url = '/sites/team/Shared Documents/big_buck_bunny.mp4'
9+
file_url = "/sites/team/Shared Documents/report #123.csv"
910
download_path = os.path.join(tempfile.mkdtemp(), os.path.basename(file_url))
1011
with open(download_path, "wb") as local_file:
11-
file = ctx.web.get_file_by_server_relative_url(file_url).download(local_file).execute_query()
12+
file = ctx.web.get_file_by_server_relative_path(file_url).download(local_file).execute_query()
1213
print("[Ok] file has been downloaded: {0}".format(download_path))

examples/sharepoint/files/upload_file.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import os
22

33
from office365.sharepoint.client_context import ClientContext
4-
from tests import test_site_url, test_user_credentials
4+
from tests import test_site_url, test_user_credentials, test_team_site_url
55

6-
ctx = ClientContext(test_site_url).with_credentials(test_user_credentials)
6+
ctx = ClientContext(test_team_site_url).with_credentials(test_user_credentials)
77

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

examples/sharepoint/files/upload_large_file.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import os
22

33
from office365.sharepoint.client_context import ClientContext
4-
from tests import test_site_url, test_user_credentials
4+
from tests import test_site_url, test_user_credentials, test_team_site_url
55

6-
ctx = ClientContext(test_site_url).with_credentials(test_user_credentials)
6+
ctx = ClientContext(test_team_site_url).with_credentials(test_user_credentials)
77

8-
target_url = "/Shared Documents"
8+
target_url = "/sites/team/Shared Documents"
99
target_folder = ctx.web.get_folder_by_server_relative_url(target_url)
1010
size_chunk = 1000000
1111
local_path = "../../../tests/data/big_buck_bunny.mp4"

office365/sharepoint/files/file.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ def save_binary(ctx, server_relative_url, content):
270270
:type server_relative_url: str
271271
:type content: str
272272
"""
273-
url = r"{0}web/getFileByServerRelativeUrl('{1}')/\$value".format(
273+
url = r"{0}web/getFileByServerRelativePath(DecodedUrl='{1}')/\$value".format(
274274
ctx.service_root_url(), server_relative_url)
275275
request = RequestOptions(url)
276276
request.method = HttpMethod.Post
@@ -288,7 +288,8 @@ def open_binary(ctx, server_relative_url):
288288
:type server_relative_url: str
289289
:return Response
290290
"""
291-
url = r"{0}web/getfilebyserverrelativeurl('{1}')/\$value".format(ctx.service_root_url(), server_relative_url)
291+
url = r"{0}web/getFileByServerRelativePath(DecodedUrl='{1}')/\$value".format(ctx.service_root_url(),
292+
server_relative_url)
292293
request = RequestOptions(url)
293294
request.method = HttpMethod.Get
294295
response = ctx.execute_request_direct(request)

office365/sharepoint/webs/web.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,17 @@ def get_file_by_server_relative_url(self, url):
249249
ResourcePathServiceOperation("getFileByServerRelativeUrl", [url], self.resource_path)
250250
)
251251

252+
def get_file_by_server_relative_path(self, decoded_url):
253+
"""Returns the file object located at the specified server-relative path.
254+
Prefer this method over get_folder_by_server_relative_url since it supports % and # symbols in names
255+
256+
:type decoded_url: str
257+
"""
258+
return File(
259+
self.context,
260+
ResourcePathServiceOperation("getFileByServerRelativePath", {"DecodedUrl": decoded_url}, self.resource_path)
261+
)
262+
252263
def get_folder_by_server_relative_url(self, url):
253264
"""Returns the folder object located at the specified server-relative URL.
254265
:type url: str
@@ -258,6 +269,19 @@ def get_folder_by_server_relative_url(self, url):
258269
ResourcePathServiceOperation("getFolderByServerRelativeUrl", [url], self.resource_path)
259270
)
260271

272+
def get_folder_by_server_relative_path(self, decoded_url):
273+
"""Returns the folder object located at the specified server-relative URL.
274+
Prefer this method over get_folder_by_server_relative_url since it supports % and # symbols
275+
Details: https://docs.microsoft.com/en-us/sharepoint/dev/solution-guidance/supporting-and-in-file-and-folder-with-the-resourcepath-api
276+
277+
:type decoded_url: str
278+
"""
279+
params = {"DecodedUrl": decoded_url}
280+
return File(
281+
self.context,
282+
ResourcePathServiceOperation("getFolderByServerRelativePath", params, self.resource_path)
283+
)
284+
261285
def ensure_folder_path(self, path):
262286
"""
263287
Ensures a nested folder hierarchy exist

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
setup(
1212
name="Office365-REST-Python-Client",
13-
version="2.3.3",
13+
version="2.3.4",
1414
author="Vadim Gremyachev",
1515
author_email="[email protected]",
1616
maintainer="Konrad Gądek, Domenico Di Nicola",

0 commit comments

Comments
 (0)