Delete Lists and items from SharePoint site using CAML Query.


CAML stands for Collaborative Markup Language. CAML is used to query from SharePoint server. This is an old approach to query from server as LINQ has replace the CAML. Any way it is important to be familiar with CAML and LINQ both. In this article I will explain about CQML query. The query is about to delete the list and items.

To delete lists and items, you can use the DeleteObject method.


ClientContext context = new Microsoft.SharePoint.Client.ClientContext(
"Your sharepoint site url");
List list = context.Web.Lists.GetByTitle("New List");
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = @"
< View >
< Query >
< Where >
< IsNotNull >
< FieldRef Name='Title' / >
< /IsNotNull >
< /Where >
< /Query >
< /View > ";
ListItemCollection listItems = list.GetItems(camlQuery);
context.Load(listItems, items = > items.Include(item = > item["Title"]));
context.ExecuteQuery();
foreach (ListItem listItem in listItems.ToList())
{
listItem.DeleteObject();
}
context.ExecuteQuery();
context.Dispose();

As you know that SharePoint is based on Content database i.e. each and everything of SharePoint site will be stored in a SQL database.
Then how CAML query is getting the result as SQL database understands SQL query.
Internally CAML will be converted into SQL query.
Before running this code you need to ensure that the SharePoint for which you are going to run this code having full access to you. Otherwise you will get authentication related error.

Try this code and give your valuable feedback.


Comments



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: