none
SharePoint Batch Delete RRS feed

  • Question

  • Dear all, im facing the following challenge. I work for a company that i need to delete the sharepoint list. My code is running outside sharepoint farm thus i have only a few options . I tried deleting the sharepoint List item by item but it is not working as most of the times it returns item not found thus im down to my last option to batch delete. Deleting the lists and view is not available.Can you please provide me a working code the following code is not working as mentioned previously

                                        try
                                        {
                                            var n2 = (int) n;
                                            //  string  k1= (int) n;
                                            System.Diagnostics.Debug.WriteLine("deleting item :  " + n);
                                           
                                            System.Diagnostics.Debug.WriteLine("Start ");
                                            string url3 = "http://spsap69mrs:9999/";
                                            System.Net.Http.HttpClient client3 = new HttpClient(new HttpClientHandler() { UseDefaultCredentials = true });
                                            client3.BaseAddress = new System.Uri(url3);
                                            string cmd3 = "_api/web/lists/getbytitle('Agents%20Details')/items(" + n + ")";   //batchDelete";
                                            System.Diagnostics.Debug.WriteLine("cmd3 :  " + cmd3);

                                          client3.DefaultRequestHeaders.Add("Accept", "application/json;odata=verbose");
                                            client3.DefaultRequestHeaders.Add("ContentType", "application/json");
                                            client3.DefaultRequestHeaders.Add("X-RequestDigest", (string)EXCESS);
                                           client3.DefaultRequestHeaders.Add("IF-MATCH", "*");
                                            client3.DefaultRequestHeaders.Add("X-IF-NONE-MATCH", "*");
                                      //      client3.DefaultRequestHeaders.Add("If-Unmodified-Since", "*");
                                            client3.DefaultRequestHeaders.Add("X-HTTP-Method", "DELETE");
                                            HttpContent content3 = new StringContent("");
                                        //    content3.Headers.ContentType = new MediaTypeHeaderValue("application/JSON");
                                            var response3 = client3.PostAsync(cmd3, content3).Result;
                                            System.Diagnostics.Debug.WriteLine(" response 3 /items:" + response3);
                                            System.Diagnostics.Debug.WriteLine(" item deleted:" + n);

                                        }
                                        catch
                                        {
                                        }
          

    Monday, July 29, 2019 11:35 AM

All replies

  • Hi,

    If you want to delete SharePoint list outside from the SharePoint server, we can use CSOM C# to achieve it.

    The following code for your reference.

    using System;  
    using System.Collections.Generic;  
    using System.Linq;  
    using System.Text;  
    using System.Threading.Tasks;  
    using Microsoft.SharePoint.Client;  
    namespace DeleteList  
    {  
    	class Program  
    	{  
    		static void Main(string[] args)  
    		{  
    			// ClientContext - Get the context for the SharePoint Site  
    			ClientContext clientContext = new ClientContext("http://sp2013"); 		
    			//pass the credential
    			clientContext.Credentials = new System.Net.NetworkCredential("username", "password", "domainName");
    			// Get the SharePoint web  
    			Web web = clientContext.Web;  
    			// Get the SharePoint list by Title  
    			List list = web.Lists.GetByTitle("NewList");  
    			// Delete the list object  
    			list.DeleteObject();  
    			// Execute the query to the server.  
    			clientContext.ExecuteQuery();  
    		}
    	}	
    } 

    More information:

    https://www.c-sharpcorner.com/blogs/delete-a-list-from-the-sharepoint-2013-using-csom

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Tuesday, July 30, 2019 1:50 AM
    Moderator
  • Hi,

    Would you please provide us with an update on the status of your issue?

    Best Regards,
    Dennis

    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Monday, August 5, 2019 8:48 AM
    Moderator