Vikram Babu Nagineni
Vikram Babu Nagineni

Reputation: 3549

How to delete attachment from list item in sharepoint using client object model?

The AttachmentCollection object does not have any delete method. How can i do it?

Upvotes: 1

Views: 4866

Answers (1)

Vadim Gremyachev
Vadim Gremyachev

Reputation: 59338

AttachmentCollection class does not expose any methods for deleting attachments, but you could utilize Attachment.DeleteObject method to delete an Attachment from a collection.

The following example demonstrates how to delete all attachments in list item:

public static void DeleteAttachmentFiles(ClientContext context, string listTitle,int listItemId)
{
    var list = context.Web.Lists.GetByTitle(listTitle);
    var listItem = list.GetItemById(listItemId);
    context.Load(listItem, li => li.AttachmentFiles);
    context.ExecuteQuery();
    listItem.AttachmentFiles.ToList().ForEach(a => a.DeleteObject()); 
    context.ExecuteQuery();
}

Upvotes: 5

Related Questions