SharePoint CSOM delete all list items

How to delete items via CSOM, but use the Title to find the item and then delete it

Archived Forums
>
SharePoint 2013 - Development and Programming
  • Question

  • 0
    Sign in to vote

    Hello guys,

    I have one SP DocLib and a List. When an item is created in the Doclib, it will create an entry in the list with the Title and the reference ID from the DocLib.

    I tried to delete the item in den DocLib via itemID and in the list and would like to find it via reference ID and delete it in one step. The deletion int the DocLib works, but not in the List, because I only can find the right item via the reference ID and not via item ID.

    I would like to use CSOM or Rest.

    I hope that you can help me!

    Best regards

    Matthias

All replies

  • 0
    Sign in to vote

    Hi,

    You can use either code

    //social.msdn.microsoft.com/Forums/office/en-US/998cef12-293d-4fb1-b50e-d1baa1e22778/delete-bulk-items-sharepoint-2013-using-csom?forum=sharepointgeneral

    Or

    //stackoverflow.com/questions/31452356/sharepoint-2013-clientcontext-how-to-delete-specific-list-items-by-multiple-co

    KRISHANAKUMAR

    SharePoint Architect

    Mosstechnet-kk.com

    Please click"Mark As Answer"if this post solves your problem or"Vote As Helpful"if it was useful

  • 0
    Sign in to vote

    Hi,

    I have written a demo below for your reference:

    using Microsoft.SharePoint.Client; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplicationDeleteItem { class Program { static void Main(string[] args) { //load Lists ClientContext ctx = new ClientContext("//wakaka:17710/sites/hello_world"); Web web = ctx.Web; List TestList = web.Lists.GetByTitle("TestList"); List DocLib = web.Lists.GetByTitle("DocLib"); ctx.ExecuteQuery(); //Delete item via id in DocLib and I assume the Id is 1 int DocLibID = 1; ListItem DocLibItem = DocLib.GetItemById(DocLibID); DocLibItem.DeleteObject(); ctx.ExecuteQuery(); //Delete item via ReferenceId in TestList CamlQuery query = new CamlQuery(); ListItemCollection collection = TestList.GetItems(query); ctx.Load(collection); ctx.ExecuteQuery(); foreach (ListItem item in collection) { Object ReferenceId = item["ReferenceId"]; if (int.Parse(ReferenceId.ToString()) == DocLibID) { item.DeleteObject(); } ctx.ExecuteQuery(); Console.WriteLine("delete success"); Console.ReadLine(); return; } } } }

    The better way is using "Lookup" column and setting Association.

    Best regards,

    Lee Liu


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact


    • Proposed as answer by
    • Edited by text

Video liên quan

Chủ đề