2
2
import os
3
3
4
4
from office365 .runtime .client_request import ClientRequest
5
+ from office365 .runtime .utilities .http_method import HttpMethod
5
6
from office365 .runtime .utilities .request_options import RequestOptions
6
7
from office365 .sharepoint .caml_query import CamlQuery
8
+ from office365 .sharepoint .list_data_service import ListDataService
7
9
from settings import settings
8
10
from office365 .runtime .auth .authentication_context import AuthenticationContext
9
11
from office365 .sharepoint .client_context import ClientContext
@@ -43,27 +45,27 @@ def read_folder_and_files(context, list_title):
43
45
print ("Folder name: {0}" .format (folder .properties ["Name" ]))
44
46
45
47
46
- def upload_file_into_library ( target_library , name , content ):
47
- context = target_library .context
48
+ def upload_file_alt ( target_folder , name , content ):
49
+ context = target_folder .context
48
50
info = FileCreationInformation ()
49
51
info .content = content
50
52
info .url = name
51
53
info .overwrite = True
52
- target_file = target_library . root_folder .files .add (info )
54
+ target_file = target_folder .files .add (info )
53
55
context .execute_query ()
54
56
return target_file
55
57
56
58
57
59
def upload_file (context ):
58
- upload_into_library = False
60
+ upload_into_library = True
59
61
path = "../tests/data/SharePoint User Guide.docx"
60
62
with open (path , 'rb' ) as content_file :
61
63
file_content = content_file .read ()
62
64
63
65
if upload_into_library :
64
66
list_title = "Documents"
65
- library = context .web .lists .get_by_title (list_title )
66
- file = upload_file_into_library ( library , os .path .basename (path ), file_content )
67
+ target_folder = context .web .lists .get_by_title (list_title ). root_folder
68
+ file = upload_file_alt ( target_folder , os .path .basename (path ), file_content )
67
69
print ("File url: {0}" .format (file .properties ["ServerRelativeUrl" ]))
68
70
else :
69
71
target_url = "/Shared Documents/{0}" .format (os .path .basename (path ))
@@ -77,7 +79,7 @@ def download_file(context):
77
79
78
80
79
81
if __name__ == '__main__' :
80
- site_url = 'https://mediadev8.sharepoint.com/sites/Team123 '
82
+ site_url = 'https://mediadev8.sharepoint.com/'
81
83
82
84
ctx_auth = AuthenticationContext (url = site_url )
83
85
if ctx_auth .acquire_token_for_user (username = settings ['user_credentials' ]['username' ],
@@ -89,5 +91,54 @@ def download_file(context):
89
91
# read_folder_and_files_alt(ctx, "Documents")
90
92
# upload_file(ctx)
91
93
# download_file(ctx)
94
+
95
+ file = ctx .web .get_file_by_server_relative_url ("/Shared Documents/SharePoint User Guide.docx" )
96
+ ctx .load (file )
97
+ ctx .execute_query ()
98
+
99
+ path = "../data/SharePoint User Guide.docx"
100
+ # with open(path, 'rb') as content_file:
101
+ # file_content = content_file.read()
102
+ # list_title = "Documents"
103
+ # target_list = ctx.web.lists.get_by_title(list_title)
104
+ # file = upload_file_alt(target_list.root_folder, os.path.basename(path), file_content)
105
+
106
+ # find out user id
107
+ user = ctx .
web .
site_users .
get_by_email (
"[email protected] " )
108
+ ctx .load (user )
109
+ ctx .execute_query ()
110
+ user_id = user .properties ['Id' ]
111
+ user_field_value = json .dumps ([{'Key' : user .properties ['LoginName' ]}])
112
+
113
+ # set file metadata
114
+ list_item = file .listitem_allfields # get associated listItem
115
+
116
+ field_editor = list_item .parent_list .fields .get_by_internal_name_or_title ("Modified By" )
117
+ ctx .load (field_editor )
118
+ ctx .execute_query ()
119
+
120
+ if field_editor .properties ['ReadOnlyField' ]:
121
+ field_editor .set_property ('ReadOnlyField' , False )
122
+ field_editor .update ()
123
+ ctx .execute_query ()
124
+
125
+ list_item .set_property ("EditorId" , user_id ) # update ModifiedBy field value
126
+ # list_item.set_property("ModifiedById", user_id) # update ModifiedBy field value
127
+ # list_item.set_property("Comment", 'some comment goes here212aaa..')
128
+
129
+ # field_values = [
130
+ # {"FieldName": 'Editor', "FieldValue": user_field_value},
131
+ # ]
132
+ # list_item.system_update(field_values, True)
133
+
134
+ list_svc = ListDataService (site_url , ctx_auth )
135
+ #field_values = {"Comment": "Some comment goes here"}
136
+ field_values = {"ModifiedById" : 11 }
137
+
138
+ # list_item = list_svc.get_list_item("Documents", 4)
139
+ # list_svc.load(list_item)
140
+ list_svc .update_list_item ("Documents" , 4 , field_values )
141
+ list_svc .execute_query ()
142
+ print ("Ok" )
92
143
else :
93
144
print (ctx_auth .get_last_error ())
0 commit comments