Skip to content

Commit 98bb317

Browse files
committed
SharePoint API (list & portal namespace) improvements and bug fixes
1 parent 3600a76 commit 98bb317

File tree

14 files changed

+328
-69
lines changed

14 files changed

+328
-69
lines changed

examples/sharepoint/lists/get_data_as_stream.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""
22
Returns a SharePoint List data
33
"""
4+
45
from office365.sharepoint.client_context import ClientContext
56
from tests import test_client_credentials, test_team_site_url
67

@@ -22,7 +23,7 @@
2223
"""
2324

2425

25-
result = (
26-
ctx.web.get_list_data_as_stream("/Shared Documents", view_xml=view_xml).execute_query()
27-
)
26+
result = ctx.web.get_list_data_as_stream(
27+
"/Shared Documents", view_xml=view_xml
28+
).execute_query()
2829
print(result.value)

examples/sharepoint/tenant/export_tenant_settings.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
""" """
2+
23
from office365.sharepoint.client_context import ClientContext
34
from tests import test_admin_credentials, test_admin_site_url
45

56
admin_client = ClientContext(test_admin_site_url).with_credentials(
67
test_admin_credentials
78
)
8-
result = admin_client.tenant.export_to_csv(view_xml="<View/>", list_name="Style Library").execute_query()
9+
result = admin_client.tenant.export_to_csv(
10+
view_xml="<View/>", list_name="Style Library"
11+
).execute_query()
912
print(
1013
"Sites details have been exported into {0}{1}".format(
1114
test_admin_site_url, result.value

office365/runtime/csom/request.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from office365.runtime.client_request import ClientRequest
2+
3+
4+
class CsomRequest(ClientRequest):
5+
""""""
6+
7+
def process_response(self, response, query):
8+
pass
9+
10+
def build_request(self, query):
11+
pass

office365/sharepoint/documentmanagement/document_id.py

+28-3
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,47 @@ def reset_docid_by_server_relative_path(self, decoded_url):
3131
self.context.add_query(qry)
3232
return self
3333

34-
def reset_docids_in_library(self, decoded_url, content_type_id):
34+
def reset_doc_ids_in_library(self, decoded_url, content_type_id=None):
3535
"""
3636
Performs the same function as ResetDocIdByServerRelativePath (section 3.1.5.10.2.1.1), but for every
3737
document in the specified document library.
3838
3939
:param str decoded_url: Server relative path to the document library, for which all document identifiers
4040
MUST be reset to guarantee global uniqueness in the farm.
41-
:param str content_type_id: The content type identifier.
41+
:param str or None content_type_id: The content type identifier.
4242
"""
43-
payload = {"DecodedUrl": decoded_url, "contentTypeId": content_type_id}
43+
payload = {"decodedUrl": decoded_url, "contentTypeId": content_type_id}
4444
qry = ServiceOperationQuery(
4545
self, "ResetDocIdsInLibrary", None, payload, None, None
4646
)
4747
self.context.add_query(qry)
4848
return self
4949

50+
def set_doc_id_site_prefix(
51+
self, prefix, schedule_assignment, overwrite_existing_ids
52+
):
53+
"""
54+
Allows to set or change the prefix used for Document IDs
55+
56+
:param str prefix:
57+
:param bool schedule_assignment:
58+
:param bool overwrite_existing_ids:
59+
"""
60+
payload = {
61+
"prefix": prefix,
62+
"scheduleAssignment": schedule_assignment,
63+
"overwriteExistingIds": overwrite_existing_ids,
64+
}
65+
qry = ServiceOperationQuery(
66+
self, "SetDocIdSitePrefix", None, payload, None, None
67+
)
68+
self.context.add_query(qry)
69+
return self
70+
5071
@property
5172
def entity_type_name(self):
5273
return "SP.DocumentManagement.DocumentId"
74+
75+
@property
76+
def entity_type_id(self):
77+
return "fb7276a2-cd36-448b-8a60-a589205f5a8f"

office365/sharepoint/files/versions/event.py

+9
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,12 @@ def shared_with_users(self):
3232
return self.properties.get(
3333
"SharedWithUsers", ClientValueCollection(SharedWithUser)
3434
)
35+
36+
def get_property(self, name, default_value=None):
37+
if default_value is None:
38+
property_mapping = {
39+
"SharedByUser": self.shared_by_user,
40+
"SharedWithUsers": self.shared_with_users,
41+
}
42+
default_value = property_mapping.get(name, None)
43+
return super(FileVersionEvent, self).get_property(name, default_value)

office365/sharepoint/listitems/listitem.py

+14
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from office365.sharepoint.fields.lookup_value import FieldLookupValue
1515
from office365.sharepoint.fields.multi_lookup_value import FieldMultiLookupValue
1616
from office365.sharepoint.fields.string_values import FieldStringValues
17+
from office365.sharepoint.fields.url_value import FieldUrlValue
1718
from office365.sharepoint.likes.liked_by_information import LikedByInformation
1819
from office365.sharepoint.listitems.compliance_info import ListItemComplianceInfo
1920
from office365.sharepoint.listitems.form_update_value import ListItemFormUpdateValue
@@ -507,12 +508,25 @@ def versions(self):
507508
),
508509
)
509510

511+
@property
512+
def doc_id(self):
513+
# type: () -> Optional[str]
514+
"""Document ID fora document"""
515+
return self.properties.get("OData__dlc_DocId", None)
516+
517+
@property
518+
def doc_id_url(self):
519+
# type: () -> Optional[FieldUrlValue]
520+
"""Document ID fora document"""
521+
return self.properties.get("OData__dlc_DocIdUrl", FieldUrlValue())
522+
510523
def get_property(self, name, default_value=None):
511524
if default_value is None:
512525
property_mapping = {
513526
"AttachmentFiles": self.attachment_files,
514527
"ContentType": self.content_type,
515528
"ComplianceInfo": self.compliance_info,
529+
"OData__dlc_DocIdUrl": self.doc_id_url,
516530
"EffectiveBasePermissions": self.effective_base_permissions,
517531
"GetDlpPolicyTip": self.get_dlp_policy_tip,
518532
"FieldValuesAsHtml": self.field_values_as_html,
+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import json
2+
from typing import IO, AnyStr, Callable
3+
4+
from typing_extensions import Self
5+
6+
from office365.runtime.client_result import ClientResult
7+
from office365.sharepoint.files.system_object_type import FileSystemObjectType
8+
from office365.sharepoint.listitems.collection import ListItemCollection
9+
from office365.sharepoint.listitems.listitem import ListItem
10+
from office365.sharepoint.lists.list import List
11+
12+
13+
class ListExporter(object):
14+
15+
@staticmethod
16+
def export(
17+
source_list, destination_file, include_content=False, item_exported=None
18+
):
19+
# type: (List, IO, bool, Callable[[ListItem], None]) -> Self
20+
"""Exports SharePoint List"""
21+
import zipfile
22+
23+
def _append_file(name, data):
24+
with zipfile.ZipFile(
25+
destination_file.name, "a", zipfile.ZIP_DEFLATED
26+
) as zf:
27+
zf.writestr(name, data)
28+
29+
def _download_content(list_item):
30+
# type: (ListItem) -> None
31+
def _after_downloaded(result):
32+
# type: (ClientResult[AnyStr]) -> None
33+
item_path = list_item.properties["FileRef"].replace(
34+
source_list.root_folder.serverRelativeUrl, ""
35+
)
36+
_append_file(item_path, result.value)
37+
38+
list_item.file.get_content().after_execute(_after_downloaded)
39+
40+
def _export_items(items):
41+
# type: (ListItemCollection) -> None
42+
43+
for item in items:
44+
item_path = str(item.id) + ".json"
45+
46+
if item.file_system_object_type == FileSystemObjectType.File:
47+
_append_file(item_path, json.dumps(item.to_json()))
48+
49+
if include_content:
50+
_download_content(item)
51+
52+
if callable(item_exported):
53+
item_exported(item)
54+
55+
def _get_items():
56+
(
57+
source_list.items.select(
58+
[
59+
"*",
60+
"Id",
61+
"FileRef",
62+
"FileDirRef",
63+
"FileLeafRef",
64+
"FileSystemObjectType",
65+
]
66+
)
67+
.get()
68+
.paged(page_loaded=_export_items)
69+
)
70+
71+
source_list.ensure_properties(["SchemaXml", "RootFolder"], _get_items)
72+
73+
return source_list

0 commit comments

Comments
 (0)