In Previous articles, I explained you what is CAML and how to replace CAML with LINQ to SharePoint. Please refer this LINQ to SharePoint article if you are not aware of that.
My goal is to explain :
How to insert/read/update/delete an item in SharePoint list using LINQ
I found few more advantages to use LINQ to SharePoint during this development are as below
We can perform :
Multiple list item insertion and deletion can be performed very easily with one syntax.
Join operation can be performed very easily on the related list
We can user LINQ with
- Managed application , like windows or console or WPF
- With SharePoint client object models.
- With SharePoint Web Parts.
- With SPGridView.
Before pitching into CRUD functionality, we need to create entity class using SPMetal.
I am assuming you read previous articles of LINQ to SharePoint which described you how to use SPMetal.exe (created MySPLinqEntities.cs entity class) and create entity class.
Creat Custom list:
I have created one custom list with name “Projects” as below:
List with Column names:
List with default values:
Read the items from list using LINQ:
Code snippet to retrieve all records from Projects list using LINQ is as below:
And output of above code is as below (I am binding all records into GridView control)
Insert record to SharePoint list using LINQ:
Code snippet to insert record into Projects list using LINQ is as below:
Once user clicks on “Save” button, record will be saved into Projects list.
Now our Projects list will have newly added record as below
Update a Particular list item in SharePoint list using LINQ:
Code snippet to update record into Projects list using LINQ is as below:
Delete a Particular list item in SharePoint list using LINQ:
Code snippet to delete record into Projects list using LINQ is as below:
I am done with it now. This is basics for CRUD operations. Happy programming!!!!!!!