Office365-rest-python-client: How to add a string and attachment in the same sharepoint's ListItem?

Created on 16 Nov 2020  ·  2Comments  ·  Source: vgrem/Office365-REST-Python-Client

Hi @vgrem @Toilal,

I was looking to your code and trying to figure out a code that create a new line in a sharepoint list adding the "request id" string and its attachment file.

This is what I need to do:
question_upload_file_sharepoint

As the red arrows shows I need to add the "REQUEST_ID" string and add the "Attachments" file.

I saw some code to upload file but did not find anything related to attachments.

Any help is very welcome!

TKS

question

All 2 comments

Greetings @libora6 ,

here is a generic example which demonstrates how to:

  • create a list item
  • upload an attachment into list item
ctx = ClientContext(site_url).with_credentials(ClientCredential(client_id, client_secret))
list_object = ctx.web.lists.get_by_title(list_title)
# 1. create a list item first
new_item = list_object.add_item({
    "Title": "Task 123"
}).execute_query()
# 2. upload attachment into list item
with open(attachment_path, 'rb') as content_file:
    file_content = content_file.read()
attachment_file_name = os.path.basename(attachment_path)
attachment_file_information = AttachmentfileCreationInformation(attachment_file_name, file_content)
attachment_file = new_item.attachmentFiles.add(attachment_file_information).execute_query()

Notes:

  • In the provided example only Title is specified for list item, in your case make sure to specify proper name for field REQUEST_ID

Result

image

@vgrem tks for your prompt support.

Was this page helpful?
0 / 5 - 0 ratings