-
-
Notifications
You must be signed in to change notification settings - Fork 370
Expand file tree
/
Copy pathprovision_fields.py
More file actions
54 lines (42 loc) · 1.91 KB
/
Copy pathprovision_fields.py
File metadata and controls
54 lines (42 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
"""
Provision multiple typed fields on a list from a schema specification.
Maps a name -> FieldType schema (like migration tools do) and creates
each field via the generic FieldCreationInformation.
https://learn.microsoft.com/en-us/sharepoint/dev/apis/rest-api
"""
import argparse
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.fields.creation_information import FieldCreationInformation
from office365.sharepoint.fields.type import FieldType
from tests.settings import cert_path, cert_thumbprint, client_id, site_url, tenant
LIST_TITLE = "Tasks"
FIELDS = {
"CustomerName": FieldType.Text,
"Quantity": FieldType.Number,
"DueDate": FieldType.DateTime,
"Status": FieldType.Choice,
"Notes": FieldType.Note,
}
def main():
parser = argparse.ArgumentParser(description="Provision typed fields from a schema spec")
parser.add_argument("--list-title", default=LIST_TITLE, help="Target list")
parser.add_argument("--keep", action="store_true", help="Keep created fields (default: delete after demo)")
args = parser.parse_args()
ctx = ClientContext(site_url).with_client_certificate(
tenant, client_id=client_id, thumbprint=cert_thumbprint, cert_path=cert_path
)
target_fields = ctx.web.lists.get_by_title(args.list_title).fields
created = []
for name, field_type in FIELDS.items():
info = FieldCreationInformation(Title=name, FieldTypeKind=field_type)
if field_type == FieldType.Choice:
info.Choices = ["Not Started", "In Progress", "Completed", "Deferred"]
field = target_fields.add_field(info).execute_query()
created.append(field)
print(f" created {name:16s} ({field_type.name}) -> {field.internal_name}")
if not args.keep:
for field in created:
field.delete_object().execute_query()
print(" (fields removed after demo)")
if __name__ == "__main__":
main()