0

Will the ListItem's attachments also be deleted if I call the ListItem.DeleteObject method which deletes my item? Or are the attachment files still on my server? How can I check this? I use the client object model!

D. Müller
  • 3,336
  • 4
  • 36
  • 84

1 Answers1

0

Yes, attachments associated with list item are getting deleted once the ListItem.DeleteObject method is invoked and the query is submitted to the server via ClientContext.ExecuteQuery method.

The following example demonstrates how to verify whether attachments have been deleted:

//First, lets delete list item that contains attachments 
var list = ctx.Web.Lists.GetByTitle(listTitle);
var item = list.GetItemById(itemId);
ctx.Load(list.RootFolder, f => f.ServerRelativeUrl);
item.DeleteObject(); //delete list item operation
ctx.ExecuteQuery();

//Then, let's verify whether associated attachment file(s) have been deleted  
var attachamentRootFolderUrl = string.Format("{0}/Attachments",list.RootFolder.ServerRelativeUrl);
var attachamentFolderUrl = string.Format("{0}/{1}", attachamentRootFolderUrl, itemId);
var folder = ctx.Web.GetFolderByServerRelativeUrl(attachamentRootFolderUrl);
var result = ctx.LoadQuery(folder.Folders.Where( f => f.ServerRelativeUrl == attachamentFolderUrl));
ctx.ExecuteQuery();
if (!result.Any())
{
     Console.WriteLine("Attachaments have been deleted.");
}  
Vadim Gremyachev
  • 57,952
  • 20
  • 129
  • 193