---
title: "Insert, Update, Delete in SharePoint 2010 using LINQ"  
description: "In SharePoint 2010 we can use LINQ syntax to query the list instead of using CAML query. In order to work with LINQ we need a command line tool called"  
author: "Chris Anderson"  
published: 2011-12-12  
updated: 2020-05-04  
canonical: https://www.mindstick.com/articles/844/insert-update-delete-in-sharepoint-2010-using-linq  
category: "sharepoint"  
tags: ["sharepoint"]  
reading_time: 3 minutes  

---

# Insert, Update, Delete in SharePoint 2010 using LINQ

In SharePoint 2010 we can use LINQ syntax to query the list instead of using CAML query. In order to work with LINQ we need a [command line](https://www.mindstick.com/forum/10/is-there-a-method-to-get-command-line-arguments-in-vb-dot-net) tool called SPMetal.exe.

This tool is used to generate the entity classes that is required to perform [object oriented](https://www.mindstick.com/articles/12072/features-of-java-simple-small-and-object-oriented) queries towards [SharePoint server](https://www.mindstick.com/forum/320/sharepoint-server-displays-a-blank-page-after-installing-sharepoint). It is also required to get the intellisense when we are [working in Visual](https://answers.mindstick.com/qa/115739/why-is-intellisense-not-working-in-visual-studio-2026) Studio 2010.This tool resides in **C:\Program Files\Common Files\Microsoft Shared\[Web Server](https://www.mindstick.com/articles/23199/digital-personal-server-the-premium-web-server) Extensions\14\BIN** folder.

##### Creating the entity classes:

- Open [Command Prompt](https://answers.mindstick.com/qa/51716/how-to-make-bootable-pendrive-using-cmd-windows-command-prompt) as an administrator.
- Change the path to C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\BIN.

![Insert, Update, Delete in SharePoint 2010 using LINQ](https://www.mindstick.com/mindstickarticle/a2b6dc4a-9f21-42c5-b76d-05154f1d82aa/images/00f82e72-295f-4065-ac07-3260e2f75e08.png)

- Run the following command to generate the entity classes.\ \ **SPMetal.exe /web:http://sharepointsiteaddress /code:d:\YourEntityFile.cs**

![Insert, Update, Delete in SharePoint 2010 using LINQ](https://www.mindstick.com/mindstickarticle/a2b6dc4a-9f21-42c5-b76d-05154f1d82aa/images/9545a15f-4190-4105-9139-7f9312516719.png)

- Open [Visual Studio](https://www.mindstick.com/articles/12378/visual-studio-for-mac-is-out-of-beta-preview-now-officially-available) 2010.
- Go to File à New à Project.
- Select [Console Application](https://www.mindstick.com/forum/160510/how-to-fetch-data-from-the-database-in-the-dot-net-console-application-using-c-sharp) from the installed templates.

![Insert, Update, Delete in SharePoint 2010 using LINQ](https://www.mindstick.com/mindstickarticle/a2b6dc4a-9f21-42c5-b76d-05154f1d82aa/images/5a1d1fbe-2753-4ac2-8a5c-ac6fcdf2579c.png)

- Right click on the solution, select "Add an existing item".
- Add the MyEntities.cs class to the solution.

![Insert, Update, Delete in SharePoint 2010 using LINQ](https://www.mindstick.com/mindstickarticle/a2b6dc4a-9f21-42c5-b76d-05154f1d82aa/images/d67acf07-8332-4f02-be86-36afc549f612.png)

· Add References by right click on the **Reference** option:

· Choose **Browse:**

· Go to the following location: **C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI****.**

· Add **Microsoft.SharePoint.dll and Microsoft.SharePoint.Linq.dll** in Reference.

![Insert, Update, Delete in SharePoint 2010 using LINQ](https://www.mindstick.com/mindstickarticle/a2b6dc4a-9f21-42c5-b76d-05154f1d82aa/images/e7fcfef0-d81e-454b-a259-d16dbe9fe3b7.png)

I have a following Products list in the SharePoint Server.

![Insert, Update, Delete in SharePoint 2010 using LINQ](https://www.mindstick.com/mindstickarticle/a2b6dc4a-9f21-42c5-b76d-05154f1d82aa/images/af74e80e-d099-4bcf-b6c1-df193387abba.png)

Add the following code in Program.cs file to perform DML operation in SharePoint list:

```
using System;using System.Collections.Generic;using System.Linq;using System.Text;using Microsoft.SharePoint.Client;using Microsoft.SharePoint.Linq; namespace DMLLinqinSP{    class Program    {        private void Insert()        {            MyEntitiesDataContext myEntitiesDataContext = new                                  
                                   MyEntitiesDataContext("http://rohit:34143/");             // Get the list from the site            EntityList<ProductsItem> listItems =  myEntitiesDataContext.GetList< myEntitiesDataContext.GetList<ProductsItem>("Products");             //Create a new item            ProductsItem newItem = new ProductsItem()            {                Title= "Hardware",                ProductID= "5",                ProductName= "RAM"            };             // Insert the new list item to the list            listItems.InsertOnSubmit(newItem);             //Submit the changes            myEntitiesDataContext.SubmitChanges();            Console.WriteLine("Item Inserted");        }        private void Update()        {            MyEntitiesDataContext myEntitiesDataContext = new 
                                     
                                     MyEntitiesDataContext("http://rohit:34143/");            // Querying the list item that has to
be updated            var updateItem = (from item in myEntitiesDataContext.Products where item.ProductID== item.ProductID== "1" select item).First();            updateItem.ProductID= "6";            updateItem.ProductName= "MotherBoard";             // Submit the changes            myEntitiesDataContext.SubmitChanges();             Console.WriteLine("Item Updated");        }        private void Delete()        {            // Create an instance            MyEntitiesDataContext myEntitiesDataContext = new 
                                      
                                      MyEntitiesDataContext("http://rohit:34143/");            // Get the list from the site            EntityList<ProductsItem> listItems = myEntitiesDataContext.GetList< myEntitiesDataContext.GetList<ProductsItem>("Products");             // Querying the list item that has to be deleted            var updateItem = (from item in myEntitiesDataContext.Products where  item.ProductID== item.ProductID== "6" select item).First();             // Deleting the list item            listItems.DeleteOnSubmit(updateItem);             // Submit the changes                        myEntitiesDataContext.SubmitChanges();             Console.WriteLine("Item Deleted");        }        static void Main(string[] args)        {            Program obj = new Program();            byte ch = 0;            do            {                Console.WriteLine("\t\t\t----Select option----\t\t\t");                Console.WriteLine("\t\t\t----Press (1) For Insert ----\t\t\t");                Console.WriteLine("\t\t\t----Press (2) For Update ----\t\t\t");         
      Console.WriteLine("\t\t\t----Press (3) For Delete ----\t\t\t");                Console.WriteLine("\t\t\t----Press (0) For Exit ----\t\t\t");                ch = Convert.ToByte(Console.ReadLine());                switch (ch)                {                    case 1:                        obj.Insert();                        break;                    case 2:                        obj.Update();                        break;                    case 3:                        obj.Delete();                        break;                    default:                        Console.WriteLine("Invalid Option");                        break;                }            } while (ch != 0);        }    }}
```

After creating an application press F5 to debug [your application](https://answers.mindstick.com/qa/95487/how-do-you-troubleshoot-when-your-application-link-is-not-responding):

![Insert, Update, Delete in SharePoint 2010 using LINQ](https://www.mindstick.com/mindstickarticle/a2b6dc4a-9f21-42c5-b76d-05154f1d82aa/images/94a287bc-2393-41ca-8539-71a035770793.png)

· When you Press (1) specified item is added in the SharePoint list. Simultaneously, you can also check in the SharePoint list (Products) that one row is added in the list.

· When you Press (2) Product ID **1** is updated in SharePoint Products list.

· When you Press (3) Product ID **6** is deleted from the SharePoint Products list.

Thanks for reading this article. I think this will help you a lot.

\

---

Original Source: https://www.mindstick.com/articles/844/insert-update-delete-in-sharepoint-2010-using-linq

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
