Linq To Sharepoint for Beginners

Hello 🙂

In SharePoint 2010 you now have the ability to use LINQ syntax to fetch items from your lists instead of using the “traditional” approach of CAML queries.

In this Post I will show you how you can start using LINQ queries in SharePoint, also known as LINQ to SharePoint.

So let’s Get Started 😉

1. creating a sample Web Part 

first of all open Visual studio 2010 as an admin.

now Create an empty Sharepoint project and name it LinqToSharePointProject.

Right Click on the project choose add and New Item

choose VisualWebPart and Name it LINQWebPart

now : Add proper references; in order to use LINQ to SharePoint, you also need to reference the Microsoft.SharePoint.Linqassembly. Point to references, right-click and choose “Add Reference” and select the Microsoft.SharePoint.Linq.dll file:

Add the following statement to the top of the LINQWebPartUserControl.ascx.cs/vb file to reference the necessary assemblies:

2. Using SPMetal.exe :

Using the tool called SPMetal, we generate our entity-classes that are needed to perform these object oriented queries toward our SharePoint server.

First launch a cmd-window and navigate to C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions14bin by using cd C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\BIN

and then run the following command to utilize the SPMetal.exe tool with the following syntax:

SPMetal /web:http://yousite /code:Entities.cs /language:csharp

for example: SPMetal /web:http://win-5g4msqldlt8 /code:C:\Entities.cs /language:csharp

Locate the file named Entities.cs that was created in the previous step to make sure that is was created.

now back to visual studio to add this file to the project.

Add the Entities.cs file you created in the previous exercise to the project by right-clicking the project and selecting Add » Existing Item.

choose  the file Entities.cs.

3. Ready to code?

Up until now We:

  1. Generated the entities using the SPMetal.exe tool
  2. Referenced the newly created file from the SharePoint project
  3. Made sure we’re using the proper references for System.Linq and Microsoft.SharePoint.Linq
  4. Be ready to code:-).

Example : i created a simple list for this demo ( before i genereted the entities file of course). it’s named Employee Review. you can create this list or you can use any list available to you.

now we are going to Write the code for the Visual WebPart User to access the list data.

In Visual Studio 2010 open the LINQWebPartUserControl.ascx file from the Solution Folder by double-clicking on it. then  Add the following markup under the <%@ Control… declaration

<%@ Import Namespace=”Microsoft.SharePoint.WebControls” %>
<SharePoint:SPGridView id=”spGridView” runat=”server” AutoGenerateColumns=”false”>

<HeaderStyle HorizontalAlign=”Left” ForeColor=”Navy” Font-Bold=”true” />


<SharePoint:SPBoundField DataField=”Title” HeaderText=”Title”></SharePoint:SPBoundField>

<SharePoint:SPBoundField DataField=”Rating” HeaderText=”Rating”></SharePoint:SPBoundField>

<SharePoint:SPBoundField DataField=”Comments” HeaderText=”Comments”></SharePoint:SPBoundField>


In the Solution Explorer, expand the carat icon next to theLINQWebPartUserControl.asx, and then double-click on theLINQWEBoPartUserControl.ascx.cs file to open it.

Add the following code in the existing Page_Load method:

protected void Page_Load(object sender, EventArgs e)
try { using (EntitiesDataContext dc = new EntitiesDataContext(“http://win-5g4msqldlt8&#8221;))
EntityList<EmployeeReviewItem> Employees = dc.GetList<EmployeeReviewItem>(“Employee Review”);
var empQuery = from emp in Employees

select new

spGridView.DataSource = empQuery;

catch (Exception x) { }
finally { }

Final Task: Deploy and Test the WebPart:

Right click on your project and select Deploy. now follow these steps to Add the web part to a web part page:

first Click the Edit button to put the page in edit mode.

then Click Insert =>Web Part => Custom => LINQWebPart Title => Add

The rendered web part will look like this.

Well Done !!!!!!!!!!!!

Exercise Summary:

In this walkthrough you built and deployed a Visual Web Part that uses LINQ to SharePoint to gather data from a SharePoint list.


Leave a Reply

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

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

Google+ photo

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

Twitter picture

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

Facebook photo

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


Connecting to %s