-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathListExportSPO.py
113 lines (97 loc) · 4.02 KB
/
ListExportSPO.py
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
"""
.NOTES
===========================================================================
Created with: PyCharm
Created on: 10/06/2020 1:46 PM
Created by: Vikas Sukhija
Organization:
Filename: ListExportSPO.py
===========================================================================
.DESCRIPTION
This script can run and export spo list items
Libraries used: Office365-REST-Python-Client
"""
import csv
from office365.runtime.auth.user_credential import UserCredential
from office365.sharepoint.client_context import ClientContext
# Load generic functions
# Write_log
def write_log(message, file_path, severity="Information"):
sev_types = ['Information', 'Warning', 'Error']
if severity not in sev_types:
raise ValueError("Invalid severity type. Expected one of: %s" % sev_types)
import datetime
d = datetime.datetime.today()
d_formatdate = d.strftime('%m-%d-%Y-%I-%M-%S')
t_green = '\033[32m' # Green Text
t_red = '\033[31m' # Red Text
t_yellow = '\033[33m' # Red Text
import os
if severity == "Information":
if not os.path.isfile(file_path):
raise ValueError("Invalid operation: %s not found" % file_path)
else:
f = open(file_path, "a")
f.write("\n")
f.write(d_formatdate + " | " + message + " | " + severity + "|")
print(t_green + d_formatdate + " | " + message + " | " + severity + "|" + t_green)
f.close()
if severity == "Warning":
if not os.path.isfile(file_path):
raise ValueError("Invalid operation: %s not found" % file_path)
else:
f = open(file_path, "a")
f.write("\n")
f.write(d_formatdate + " | " + message + " | " + severity + "|")
print(t_yellow + d_formatdate + " | " + message + " | " + severity + "|" + t_yellow)
f.close()
if severity == "Error":
if not os.path.isfile(file_path):
raise ValueError("Invalid operation: %s not found" % file_path)
else:
f = open(file_path, "a")
f.write("\n")
f.write(d_formatdate + " | " + message + " | " + severity + "|")
print(t_red + d_formatdate + " | " + message + " | " + severity + "|" + t_red)
f.close()
# Write_log
# create_file
def create_file(folder_name, name, ext):
import datetime
d = datetime.datetime.today()
d_formatdate = d.strftime('%m-%d-%Y-%I-%M-%S')
filename = folder_name + "\\" + name + "_" + d_formatdate + "_." + ext
import os
if not os.path.exists(folder_name):
os.mkdir(folder_name)
f = open(filename, "x")
f.close()
return filename
# create_file
# log and variables
log_file = create_file("logs", "spo_list_item", "log")
report_file = create_file("report", "spo_list_item", "csv")
site_url = "https://techwizard.sharepoint.com/sites/TeamSIte/"
sp_list = "DL Modification"
admin_user = "[email protected]"
admin_password = "password"
write_log("start.....script", log_file)
try:
ctx = ClientContext(site_url).with_credentials(UserCredential(admin_user, admin_password))
sp_lists = ctx.web.lists
s_list = sp_lists.get_by_title(sp_list)
l_items = s_list.get_items()
ctx.load(l_items)
ctx.execute_query()
write_log("Fetched list items", log_file)
with open(report_file, mode='w', newline='', encoding='utf-8') as csv_file:
fieldnames = ['ID', 'Title', 'Check']
writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
writer.writeheader()
for item in l_items:
write_log("exporting..." + str(item.properties['ID']), log_file)
writer.writerow({'ID': item.properties['ID'], 'Title': item.properties['Title'],
'Check': item.properties['Check']})
except:
write_log("Exception ....... caught", log_file, severity="Error")
write_log("Script.......Finished", log_file)