Here is Part 1In this article we will be seeing how to use LINQ in SharePoint 2010 to add, update and delete the list item. In SharePoint 2010 you have the ability to 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 SPMetal.exe. This tool is used to generate the entity classes that is required to perform object oriented queries towards SharePoint server. It is also required to get the intellisense when we are working in Visual Studio 2010.This tool resides in 14\bin folder. Creating the entity classes:
Inserting a new list item using LINQ:
// Get the list from the site EntityList<Item> listItems = myEntitiesDataContext.GetList<Item>("CustomList");
//Create a new item Item newItem=new Item () { Title="Test3" }; // Insert the new list item to the list listItems.InsertOnSubmit(newItem);
//Submit the changes myEntitiesDataContext.SubmitChanges(); } }
Updating a list item:
// Querying the list item that has to be updated var updateItem=(from item in myEntitiesDataContext.CustomList where item.Title=="Test1" select item).First(); updateItem.Title = "UpdatedItem";
// Submit the changes myEntitiesDataContext.SubmitChanges(); } }
Deleting the list item:
// Querying the list item that has to be deleted var updateItem=(from item in myEntitiesDataContext.CustomList where item.Title=="Test3" select item).First();
// Deleting the list item listItems.DeleteOnSubmit(updateItem);
You need to be a premium member to use this feature. To access it, you'll have to upgrade your membership.
Become a sharper developer and jumpstart your career.
$0
$
. 00
monthly
For Basic members:
$20
For Premium members:
$45
For Elite members: