Delete All Attachments From the SharePoint List Item

 Delete All Attachments From the SharePoint List Item


This example will show you how to delete Attachments from the List Item.

Please follow the steps to execute the code in visual studio using Console Application.




Steps: 
  1. Open your Visual Studio.
  2. From the template select Console Application and click next.
  3. Enter name and select the location to store the project and click create.
  4. Now your program.cs file will open, write/copy, paste the below-given code.
using Microsoft.SharePoint.Client;
using System;

namespace CSOMCRUD 
 { 
  class Program
   { 
try
     {
        using (ClientContext clientContext = new ClientContext("http://hr.zubaircorp.com/"))
            {
                Web web = clientContext.Web;
                List list = web.Lists.GetByTitle("Employeebasic");
                ListItem listItem = list.GetItemById(200);
                clientContext.Load(listItem);
                clientContext.ExecuteQuery();

                AttachmentCollection attachments = listItem.AttachmentFiles;
                clientContext.Load(attachments);
                clientContext.ExecuteQuery();

                foreach (Attachment oAttachment in attachments)
                {
                    oAttachment.DeleteObject();
                }
                clientContext.ExecuteQuery();
                Console.ReadKey();
            }

      catch (Exception ex)
        {
           throw;
        }
    }
 }

5. Click F5 or Run the Application.


Thank You for reading. Hope you have enjoyed this! :-)

Comments