sharepointsharepoint-appssharepoint-clientobject

Sharepoint Online: Are attachments also delete on deleting a ListItem?


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!


Solution

  • 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.");
    }