22import os
33
44from office365 .runtime .client_request import ClientRequest
5+ from office365 .runtime .utilities .http_method import HttpMethod
56from office365 .runtime .utilities .request_options import RequestOptions
67from office365 .sharepoint .caml_query import CamlQuery
8+ from office365 .sharepoint .list_data_service import ListDataService
79from settings import settings
810from office365 .runtime .auth .authentication_context import AuthenticationContext
911from office365 .sharepoint .client_context import ClientContext
@@ -43,27 +45,27 @@ def read_folder_and_files(context, list_title):
4345 print ("Folder name: {0}" .format (folder .properties ["Name" ]))
4446
4547
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
4850 info = FileCreationInformation ()
4951 info .content = content
5052 info .url = name
5153 info .overwrite = True
52- target_file = target_library . root_folder .files .add (info )
54+ target_file = target_folder .files .add (info )
5355 context .execute_query ()
5456 return target_file
5557
5658
5759def upload_file (context ):
58- upload_into_library = False
60+ upload_into_library = True
5961 path = "../tests/data/SharePoint User Guide.docx"
6062 with open (path , 'rb' ) as content_file :
6163 file_content = content_file .read ()
6264
6365 if upload_into_library :
6466 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 )
6769 print ("File url: {0}" .format (file .properties ["ServerRelativeUrl" ]))
6870 else :
6971 target_url = "/Shared Documents/{0}" .format (os .path .basename (path ))
@@ -77,7 +79,7 @@ def download_file(context):
7779
7880
7981if __name__ == '__main__' :
80- site_url = 'https://mediadev8.sharepoint.com/sites/Team123 '
82+ site_url = 'https://mediadev8.sharepoint.com/'
8183
8284 ctx_auth = AuthenticationContext (url = site_url )
8385 if ctx_auth .acquire_token_for_user (username = settings ['user_credentials' ]['username' ],
@@ -89,5 +91,54 @@ def download_file(context):
8991 # read_folder_and_files_alt(ctx, "Documents")
9092 # upload_file(ctx)
9193 # 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" )
92143 else :
93144 print (ctx_auth .get_last_error ())
0 commit comments