Skip to content

Commit 5dbbf60

Browse files
Release 2.3.1: SharePoint namespaces (fields, tenant and etc) improvements, bug fixes (#303, #265)
1 parent d85f717 commit 5dbbf60

21 files changed

+237
-75
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import functools
21
import os
32
from settings import settings
43

@@ -13,7 +12,7 @@
1312
target_folder = ctx.web.get_folder_by_server_relative_url(target_url)
1413
size_chunk = 1000000
1514
local_path = "../../../tests/data/big_buck_bunny.mp4"
16-
15+
# local_path = "../../../tests/data/SharePoint User Guide.docx"
1716

1817
file_size = os.path.getsize(local_path)
1918

@@ -22,12 +21,6 @@ def print_upload_progress(offset):
2221
print("Uploaded '{}' bytes from '{}'...[{}%]".format(offset, file_size, round(offset / file_size * 100, 2)))
2322

2423

25-
if file_size > size_chunk:
26-
result_file = target_folder.files.create_upload_session(local_path, size_chunk, print_upload_progress)
27-
else:
28-
with open(local_path, 'rb') as content_file:
29-
file_content = content_file.read()
30-
name = os.path.basename(local_path)
31-
result_file = target_folder.upload_file(name, file_content)
24+
uploaded_file = target_folder.files.create_upload_session(local_path, size_chunk, print_upload_progress)
3225
ctx.execute_query()
33-
print('File {0} has been uploaded successfully'.format(result_file.serverRelativeUrl))
26+
print('File {0} has been uploaded successfully'.format(uploaded_file.serverRelativeUrl))

office365/sharepoint/fields/field.py

+21
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ def create_field_from_type(context, field_parameters):
5959
field.set_property(n, v)
6060
return field
6161

62+
def enable_index(self):
63+
pass
64+
65+
def disable_index(self):
66+
pass
67+
6268
def set_show_in_display_form(self, flag):
6369
"""Sets the value of the ShowInDisplayForm property for this fields.
6470
@@ -96,6 +102,21 @@ def id(self):
96102
"""
97103
return self.properties.get('Id', None)
98104

105+
@property
106+
def schema_xml(self):
107+
"""Gets a value that specifies the XML schema that defines the field.
108+
109+
:rtype: str or None
110+
"""
111+
return self.properties.get('SchemaXml', None)
112+
113+
@schema_xml.setter
114+
def schema_xml(self, val):
115+
"""Sets a value that specifies the XML schema that defines the field.
116+
117+
"""
118+
self.set_property('SchemaXml', val)
119+
99120
@property
100121
def type_as_string(self):
101122
"""Gets a value that specifies the type of the field..

office365/sharepoint/fields/field_collection.py

+55
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
from office365.runtime.resource_path_service_operation import ResourcePathServiceOperation
66
from office365.sharepoint.base_entity_collection import BaseEntityCollection
77
from office365.sharepoint.fields.field import Field
8+
from office365.sharepoint.fields.field_creation_information import FieldCreationInformation
9+
from office365.sharepoint.fields.field_type import FieldType
810
from office365.sharepoint.fields.xmlSchemaFieldCreationInformation import XmlSchemaFieldCreationInformation
911

1012

@@ -18,6 +20,59 @@ def create_typed_object(self, properties):
1820
field = super(FieldCollection, self).create_typed_object(properties)
1921
return field
2022

23+
def add_url_field(self, title, description=None):
24+
"""
25+
Adds Url field
26+
27+
:param str title:
28+
:param str or None description:
29+
:return:
30+
"""
31+
create_field_info = FieldCreationInformation(title=title,
32+
description=description,
33+
field_type_kind=FieldType.URL)
34+
return self.add(create_field_info)
35+
36+
def add_lookup_field(self, title, lookup_list_id, lookup_field_name, allow_multiple_values=False):
37+
"""
38+
Adds Lookup field
39+
40+
:param bool allow_multiple_values:
41+
:param str lookup_field_name:
42+
:param str lookup_list_id:
43+
:param str title:
44+
"""
45+
if allow_multiple_values:
46+
field_schema = f'''
47+
<Field Type="LookupMulti" Mult="TRUE" DisplayName="{title}" Required="FALSE" Hidden="TRUE" \
48+
ShowField="{lookup_field_name}" List="{{{lookup_list_id}}}" StaticName="{title}" Name="{title}">
49+
</Field>
50+
'''
51+
target_field = self.create_field_as_xml(field_schema)
52+
else:
53+
create_field_info = FieldCreationInformation(title=title,
54+
lookup_list_id=lookup_list_id,
55+
lookup_field_name=lookup_field_name,
56+
field_type_kind=FieldType.Lookup)
57+
target_field = self.add_field(create_field_info)
58+
return target_field
59+
60+
def add_choice_field(self, title, values, multiple_values=False):
61+
"""
62+
Adds Choice field
63+
64+
:param bool multiple_values:
65+
:param list[str] values:
66+
:param str title:
67+
"""
68+
fld_type = FieldType.MultiChoice if multiple_values else FieldType.Choice
69+
create_field_info = FieldCreationInformation(title, fld_type)
70+
[create_field_info.Choices.add(choice) for choice in values]
71+
return self.add_field(create_field_info)
72+
73+
def add_user_field(self):
74+
pass
75+
2176
def add(self, field_create_information):
2277
"""Adds a fields to the fields collection.
2378

office365/sharepoint/fields/field_creation_information.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ def __init__(self, title, field_type_kind, description=None,
2424
self.Title = title
2525
self.FieldTypeKind = field_type_kind
2626
self.Description = description
27-
self.Choices = ClientValueCollection(str) if field_type_kind == FieldType.MultiChoice else None
27+
self.Choices = ClientValueCollection(str) \
28+
if field_type_kind == FieldType.MultiChoice or field_type_kind == FieldType.Choice else None
2829
self.LookupListId = lookup_list_id
2930
self.LookupFieldName = lookup_field_name
3031
self.LookupWebId = lookup_web_id

office365/sharepoint/fields/field_lookup.py

+13
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,19 @@ def __init__(self, context):
77
"""Specifies a lookup field."""
88
super().__init__(context)
99

10+
@property
11+
def allow_multiple_values(self):
12+
"""Gets a value that specifies whether the lookup field allows multiple values.
13+
:rtype: bool or None
14+
"""
15+
return self.properties.get('AllowMultipleValues', None)
16+
17+
@allow_multiple_values.setter
18+
def allow_multiple_values(self, value):
19+
"""Sets a value that specifies whether the lookup field allows multiple values.
20+
"""
21+
self.set_property('AllowMultipleValues', value)
22+
1023
@property
1124
def lookup_web_id(self):
1225
"""Gets the ID of the Web site that contains the list that is the source of this field's value.

office365/sharepoint/files/file_collection.py

+15-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
import os
2+
13
from office365.runtime.client_object_collection import ClientObjectCollection
24
from office365.runtime.queries.service_operation_query import ServiceOperationQuery
35
from office365.runtime.resource_path_service_operation import ResourcePathServiceOperation
46
from office365.sharepoint.actions.create_file import CreateFileQuery
57
from office365.sharepoint.actions.upload_session import UploadSessionQuery
68
from office365.sharepoint.files.file import File
9+
from office365.sharepoint.files.file_creation_information import FileCreationInformation
710

811

912
class FileCollection(ClientObjectCollection):
@@ -28,9 +31,18 @@ def create_upload_session(self, source_path, chunk_size, chunk_uploaded=None, *c
2831
:param chunk_func_args: arguments to pass to chunk_uploaded function
2932
:return: office365.sharepoint.files.file.File
3033
"""
31-
qry = UploadSessionQuery(self, source_path, chunk_size, chunk_uploaded, chunk_func_args)
32-
self.context.add_query(qry)
33-
return qry.file
34+
file_size = os.path.getsize(source_path)
35+
if file_size > chunk_size:
36+
qry = UploadSessionQuery(self, source_path, chunk_size, chunk_uploaded, chunk_func_args)
37+
self.context.add_query(qry)
38+
return qry.file
39+
else:
40+
with open(source_path, 'rb') as content_file:
41+
file_content = content_file.read()
42+
info = FileCreationInformation(url=os.path.basename(source_path), overwrite=True, content=file_content)
43+
qry = CreateFileQuery(self, info)
44+
self.context.add_query(qry)
45+
return qry.return_type
3446

3547
def add(self, file_creation_information):
3648
"""Creates a File resource

office365/sharepoint/listitems/listitem.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
from office365.sharepoint.fields.fieldMultiLookupValue import FieldMultiLookupValue
1313
from office365.sharepoint.permissions.securable_object import SecurableObject
1414
from office365.sharepoint.sharing.externalSharingSiteOption import ExternalSharingSiteOption
15-
from office365.sharepoint.sharing.objectSharingInformation import ObjectSharingInformation
16-
from office365.sharepoint.sharing.sharingResult import SharingResult
15+
from office365.sharepoint.sharing.object_sharing_information import ObjectSharingInformation
16+
from office365.sharepoint.sharing.sharing_result import SharingResult
1717
from office365.sharepoint.ui.applicationpages.clientPeoplePickerQueryParameters import ClientPeoplePickerQueryParameters
1818
from office365.sharepoint.ui.applicationpages.clientPeoplePickerWebServiceInterface import (
1919
ClientPeoplePickerWebServiceInterface,

office365/sharepoint/sharing/objectSharingSettings.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from office365.runtime.resource_path import ResourcePath
22
from office365.sharepoint.base_entity import BaseEntity
3-
from office365.sharepoint.sharing.objectSharingInformation import ObjectSharingInformation
3+
from office365.sharepoint.sharing.object_sharing_information import ObjectSharingInformation
44

55

66
class ObjectSharingSettings(BaseEntity):

office365/sharepoint/tenant/administration/tenant.py

+22
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
from office365.runtime.client_result import ClientResult
12
from office365.runtime.client_value_collection import ClientValueCollection
23
from office365.runtime.queries.service_operation_query import ServiceOperationQuery
34
from office365.runtime.resource_path import ResourcePath
45
from office365.sharepoint.base_entity import BaseEntity
6+
from office365.sharepoint.publishing.portal_health_status import PortalHealthStatus
57
from office365.sharepoint.tenant.administration.hubSiteProperties import HubSiteProperties
68
from office365.sharepoint.tenant.administration.secondary_administrators_fields_data import \
79
SecondaryAdministratorsFieldsData
@@ -18,6 +20,26 @@ def __init__(self, context):
1820
super().__init__(context, ResourcePath("Microsoft.Online.SharePoint.TenantAdministration.Tenant"),
1921
"Microsoft.Online.SharePoint.TenantAdministration")
2022

23+
def check_tenant_licenses(self, licenses):
24+
"""
25+
Checks whether a tenant has the specified licenses.
26+
27+
:param list[str] licenses: The list of licenses to check for.
28+
:return:
29+
"""
30+
result = ClientResult(bool)
31+
params = ClientValueCollection(str, licenses)
32+
qry = ServiceOperationQuery(self, "CheckTenantLicenses", None, params, "licenses", result)
33+
self.context.add_query(qry)
34+
return result
35+
36+
def get_site_health_status(self, sourceUrl):
37+
result = ClientResult(PortalHealthStatus)
38+
params = {"sourceUrl": sourceUrl}
39+
qry = ServiceOperationQuery(self, "GetSiteHealthStatus", None, params, None, result)
40+
self.context.add_query(qry)
41+
return result
42+
2143
def get_site_secondary_administrators(self, site_id):
2244
"""
2345
Gets site collection administrators

office365/sharepoint/views/view.py

+12
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from office365.runtime.client_result import ClientResult
12
from office365.runtime.queries.delete_entity_query import DeleteEntityQuery
23
from office365.runtime.queries.service_operation_query import ServiceOperationQuery
34
from office365.runtime.resource_path import ResourcePath
@@ -45,6 +46,12 @@ def delete_object(self):
4546
self.remove_from_parent_collection()
4647
return self
4748

49+
def render_as_html(self):
50+
result = ClientResult(str)
51+
qry = ServiceOperationQuery(self, "RenderAsHtml", None, None, None, result)
52+
self.context.add_query(qry)
53+
return result
54+
4855
@property
4956
def content_type_id(self):
5057
"""Gets the identifier of the content type with which the view is associated.
@@ -96,6 +103,11 @@ def view_query(self):
96103
"""Gets or sets a value that specifies the query that is used by the list view."""
97104
return self.properties.get('ViewQuery', None)
98105

106+
@property
107+
def base_view_id(self):
108+
"""Gets a value that specifies the base view identifier of the list view."""
109+
return self.properties.get('BaseViewId', None)
110+
99111
def set_property(self, name, value, persist_changes=True):
100112
super(View, self).set_property(name, value, persist_changes)
101113
# fallback: create a new resource path

office365/sharepoint/webs/web.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
from office365.sharepoint.recyclebin.recycleBinItemCollection import RecycleBinItemCollection
3131
from office365.sharepoint.sharing.externalSharingSiteOption import ExternalSharingSiteOption
3232
from office365.sharepoint.sharing.objectSharingSettings import ObjectSharingSettings
33-
from office365.sharepoint.sharing.sharingResult import SharingResult
33+
from office365.sharepoint.sharing.sharing_result import SharingResult
3434
from office365.sharepoint.ui.applicationpages.clientPeoplePickerQueryParameters import ClientPeoplePickerQueryParameters
3535
from office365.sharepoint.ui.applicationpages.clientPeoplePickerWebServiceInterface import (
3636
ClientPeoplePickerWebServiceInterface,

setup.py

+1-1
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.0.1",
13+
version="2.3.1",
1414
author="Vadim Gremyachev",
1515
author_email="[email protected]",
1616
maintainer="Konrad Gądek, Domenico Di Nicola",

0 commit comments

Comments
 (0)