LINQ to SharePoint : CRUD operations on SharePoint List

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:
https://mysharepointsolution.files.wordpress.com/2012/03/crud_1.jpg

List with default values:
https://mysharepointsolution.files.wordpress.com/2012/03/crud_2.jpg

Read the items from list using LINQ:
Code snippet to retrieve all records from Projects list using LINQ is as below:

https://mysharepointsolution.files.wordpress.com/2012/03/crud_4.jpg

And output of above code is as below (I am binding all records into GridView control)
https://mysharepointsolution.files.wordpress.com/2012/03/crud_3.jpg

Insert record to SharePoint list using LINQ:
Code snippet to insert record into Projects list using LINQ is as below:

https://mysharepointsolution.files.wordpress.com/2012/03/crud_insert.jpg

https://mysharepointsolution.files.wordpress.com/2012/03/crud_insert_1.jpg
Once user clicks on “Save” button, record will be saved into Projects list.

Now our Projects list will have newly added record as below
https://mysharepointsolution.files.wordpress.com/2012/03/crud_insert_2.jpg

Update a Particular list item in SharePoint list using LINQ:
Code snippet to update record into Projects list using LINQ is as below:
https://mysharepointsolution.files.wordpress.com/2012/03/crud_update.jpg

Delete a Particular list item in SharePoint list using LINQ:
Code snippet to delete record into Projects list using LINQ is as below:
https://mysharepointsolution.files.wordpress.com/2012/03/crud_delete.jpg

I am done with it now. This is basics for CRUD operations. Happy programming!!!!!!!

3 comments

  1. When we choose to do the opposite of nothing, then we can do something to help others and make a difference.
    The player who spins the coin is referred to as the spinner.
    In addition, you can save the effort of coding the store functionality, virtual goods dependencies, billing interfaces
    and rewards.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s