Using the SharePoint 2010 Managed Client Object Model – Deleting Client Objects
This is a clipboard friendly version of example #12, Deleting Client Objects, from Using the SharePoint 2010 Managed Client Object Model.
This blog is inactive.
New blog: EricWhite.com/blog
using System;
using System.Linq;
using System.Collections.Generic;
using Microsoft.SharePoint.Client;
class Program
{
static void Main(string[] args)
{
ClientContext clientContext = new ClientContext("https://intranet.contoso.com");
List list = clientContext.Web.Lists.GetByTitle("Client API Test List");
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml =
@"<View>
<Query>
<Where>
<Eq>
<FieldRef Name='Category'/>
<Value Type='Text'>Test</Value>
</Eq>
</Where>
</Query>
<RowLimit>100</RowLimit>
</View>";
ListItemCollection listItems = list.GetItems(camlQuery);
clientContext.Load(
listItems,
items => items.Include(
item => item["Title"]));
clientContext.ExecuteQuery();
foreach (ListItem listItem in listItems.ToList())
listItem.DeleteObject();
clientContext.ExecuteQuery();
}
}
Following is the same example using fully qualified names for better discoverability.
using System;
using System.Linq;
using System.Collections.Generic;
using Microsoft.SharePoint.Client;
class Program
{
static void Main(string[] args)
{
Microsoft.SharePoint.Client.ClientContext clientContext = new Microsoft.SharePoint.Client.ClientContext("https://intranet.contoso.com");
Microsoft.SharePoint.Client.List list = clientContext.Web.Lists.GetByTitle("Client API Test List");
Microsoft.SharePoint.Client.CamlQuery camlQuery = new Microsoft.SharePoint.Client.CamlQuery();
camlQuery.ViewXml =
@"<View>
<Query>
<Where>
<Eq>
<FieldRef Name='Category'/>
<Value Type='Text'>Test</Value>
</Eq>
</Where>
</Query>
<RowLimit>100</RowLimit>
</View>";
Microsoft.SharePoint.Client.ListItemCollection listItems = list.GetItems(camlQuery);
clientContext.Load(
listItems,
items => items.Include(
item => item["Title"]));
clientContext.ExecuteQuery();
foreach (Microsoft.SharePoint.Client.ListItem listItem in listItems.ToList())
listItem.DeleteObject();
clientContext.ExecuteQuery();
}
}