I'm looking to add 1 (or more) attachments to various list items based on my own primary key, rather than the auto-generated item_id field SharePoint uses.

The following is the template for adding an attachment to a list item in SharePoint via SharePoint REST API:

POST https://{site_url}/_api/web/lists/getbytitle('{list_title}')/items({item_id}/AttachmentFiles/add(FileName='{file_name}')
Authorization: "Bearer " + accessToken
Content-Length: {length of request body as integer}
X-RequestDigest: "{form_digest_value}"

"Contents of file"

Is there a way change the above URL to add attachments on something other than item_id? The endpoint example below for a GET request filters based on a specified key:value pair to retrieve fields (including the item_id that the URL above specifies), but I'd like to know how to directly add attachments based on my specified field as simply as possible, reducing the number of API calls where possible.

/_api/web/lists/GetByTitle('List')/items?$filter=MyPrimaryKey eq '...'

I've attempted to construct my own URL that uses both attachment creation and various filtering, but any such attempts produce 400 errors stating the format/URL construction is invalid.

1

There are 1 answers

0
Ganesh Sanap - MVP On

Unfortunately, this is not possible using SharePoint REST API call to add attachments to list items using the filters based on custom fields/columns.

You will need to use the item ID only for adding attachments to the SharePoint list items.

(You might already know this) So, you will have to first get the item ID using the 2nd API call you mentioned:

/_api/web/lists/GetByTitle('List')/items?$filter=MyPrimaryKey eq '...'

Then add attachments to SharePoint list item using the item ID you get from above API call like mentioned in your 1st API call:

https://{site_url}/_api/web/lists/getbytitle('{list_title}')/items({item_id}/AttachmentFiles/add(FileName='{file_name}')

Documentation:

  1. Complete basic operations using SharePoint REST endpoints
  2. Working with lists and list items with REST