In this article, you will learn how to retrieve and delete attachment files in SharePoint 2016 and Office 365 using JavaScript Object Model.
SharePoint lists are designed to have multiple attachments added to a list item. Whenever an attachment is added to a list item it internally creates an attachment folder. If there is no attachment for a list item, it won’t have an attachment folder. The attachment folder for a particular item will be located at the URL: https://SiteURL/sites/Playground/Lists/ListName/Attachments/1 ‘ListName’ is the name of the list. ‘Attachments’ is the folder for all the attachments within the list.’ 1’ is the folder name that is created with the name as List item ID. If there are no attachments for a list item, we would get the error, shown below, when the attachment folder is accessed. Let’s see how we can read the attachments files and delete them using JavaScript Object Model. Retrieve Attachment Files
Delete Attachment file Once the list attachment object is retrieved, the list attachments can be deleted, using the ‘deleteObject’ method in JavaScript Object Model.
We can test this in SharePoint by adding it to the Content Editor Web part as shown below:
Now, click apply. This will delete the attachment files. We can use the script to fetch and retrieve the attachment files in a similar way to get the list of the attachment files in the console. Thus, we have seen how to retrieve the attachment files of a SharePoint list item as well as to delete the specific attachment files, using JavaScript Object Model. This has been tested with SharePoint 2016 and Office 365.
Setting Up SharePoint 2016 Multi-Server Farm In Azure