Click here to Download Document for this post
In this post we will learn how to Deleting SharePoint List items.
Here in this scenario I have chosen Console Application template because it doens’t contains any input fields to show as webpart and also to avoid GAC registration, safe controls etc., I felt it will be easy to execute the code in Console.
Click here to Download code
See below code snippet
// Deleting SharePoint list items Using Console Application Programming and Object Models
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
namespace DeleteListItem
{
class DeletingListItem
{
static void Main(string[] args)
{
using (SPSite site = new SPSite("http://URl/rf"))
{
using (SPWeb web = site.OpenWeb())
{
int count = 0;
SPList mySourceList = web.Lists["NewEmpMasterList"];
foreach (SPListItem item in mySourceList.Items)
{
try
{
string EmpId = item["Employee ID"].ToString();
string EmpName = item["Employee name"].ToString();
string active = item["Active"].ToString();
string stages = item["Stages"].ToString();
if (active == "No" || stages == "Stage 4")
{
try
{
item.Delete();
item.Update();
}
catch (Exception){}
count++;
Console.WriteLine(count + " item(s) Deleted " + EmpId);
}
}
catch (Exception){}
}
}
}
}
}
}
See below code snippet
// Deleting SharePoint list items Using Console Application Programming and Object Models
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
namespace DeleteListItem
{
class DeletingListItem
{
static void Main(string[] args)
{
using (SPSite site = new SPSite("http://URl/rf"))
{
using (SPWeb web = site.OpenWeb())
{
int count = 0;
SPList mySourceList = web.Lists["NewEmpMasterList"];
foreach (SPListItem item in mySourceList.Items)
{
try
{
string EmpId = item["Employee ID"].ToString();
string EmpName = item["Employee name"].ToString();
string active = item["Active"].ToString();
string stages = item["Stages"].ToString();
if (active == "No" || stages == "Stage 4")
{
try
{
item.Delete();
item.Update();
}
catch (Exception){}
count++;
Console.WriteLine(count + " item(s) Deleted " + EmpId);
}
}
catch (Exception){}
}
}
}
}
}
}
run the program
This no of Items to be deleted for my SharePoint List
No comments:
Post a Comment