Home > SharePoint 2010, Visual Studio 2010 > Walkthrough 1: Using LINQ to SharePoint from within a Visual web part

Walkthrough 1: Using LINQ to SharePoint from within a Visual web part

Exercise Duration: 10 minutes

Exercise Overview

This example demonstrates how to use the SharePoint LINQ provider to read data from a SharePoint list and render the data using the SPGrid web control. It shows these created in the Visual Web Part designer in Visual Studio 2010.

Task 1: Create a new Empty Project and add a Visual Web Part

1. Open Visual Studio 2010 from the Start | Programs | Visual Studio 2010 menu.

2. Create a new project by using File | New Project.

3. Pick the SharePoint | 2010 templates.

4. From the SharePoint | 2010 templates select the Visual Web Part project template.

5. Use SPLinqSolutionDemo as the name of the project.

6. Use C:\SPHOLs as the location.

7. Press OK to create the project.



8. When the SharePoint Customization Wizard dialog appears, select the site you want to deploy the Web Part to. Click the Finish button to complete the project creation process.



9. Within Solution Explorer expand VisualWebPart1 and open VisualWebPart1.webpart.

a. Set the title element to: SPLinqDemoPart Title

b. Set the description element to: SPLinqDemoPart Description



Task 2: Generate LINQ to SharePoint proxy class to access list data.

1. Start a Visual Studio 2010 Command Prompt from the Start | All Programs | Visual Studio 2010 | Visual Studio Tools |Visual Studio Command Prompt (2010) menu

2. Change the directory to C:\SPHOLS\SPLinqSolutionDemo  

cd C:\SPHOLS\SPLinqSolutionDemo

3. spmetal.exe is a command line tool that can generate C# or VB.NET classes from a SharePoint site’s list definitions. It is similar to wsdl.exe that generates a proxy from a Web Service’s WSDL file. 

4. Run the following command to generate the LINQ proxy code.

spmetal /web:http://<YourServerName&gt; /namespace:Projects /code:Projects.cs

5. Go back to Visual Studio 2010.

6. Right-click on the SPLinqSolutionDemo project in the Solution Explorer and select Add | Existing Item.

7. Browse to C:\SPHOLS\SPLinqSolutionDemo and select the Project.cs file that was generated when you ran spmetal.

8. Right-click on the SPLinqSolutionDemo in the Solution Explorer and select Add Reference.


9. Click on the Browse tab, navigate to the C:\Program Files\Common Files\Microsoft Shared\Web Server Extension\14\ISAPI folder, and select Microsoft.SharePoint.Linq.dll and select OK.




Task 3: Write the code for the Visual WebPart User to access the list data.

1. In Visual Studio 2010 open the SPLinqDemoPartUserControl.ascx file from the Solution Folder by double-clicking on it.

2. Add the following markup to SPLinqDemoPartUserControl.ascx 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="JobTitle" HeaderText="JobTitle"></SharePoint:SPBoundField>

    <SharePoint:SPBoundField DataField="ProjectTitle" HeaderText="ProjectTitle"></SharePoint:SPBoundField>

    <SharePoint:SPBoundField DataField="DueDate" HeaderText="DueDate"></SharePoint:SPBoundField>




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



4. Add the following using statements to SPLinqDemoPartUserControl.ascx.cs  file after the existing using statements.


using System.Linq;

using Projects;

using Microsoft.SharePoint;

using Microsoft.SharePoint.Linq;



5. Add the following code to the SPLinqDemoPartUserControl.ascx.cs  file (replace the existing Page_Load method).


protected void Page_Load(object sender, EventArgs e)


    ProjectsDataContext dc = new



    EntityList<EmployeesItem> Employees = 



    var empQuery = from emp in Employees

                   where emp.Project.DueDate <                    DateTime.Now.AddMonths(6)

                   select new { emp.Title, emp.JobTitle, 

                   ProjectTitle = emp.Project.Title, 

                   DueDate =                    emp.Project.DueDate.Value.ToShortDateString() };


    spGridView.DataSource = empQuery;




Task 4: Deploy and Test the WebPart

1. Right click on your project and select Deploy. 

2. Add the web part to a web part page:

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

Click Insert

Click Web Part

Click Custom

Click SPLinqDemoPart Title

Click Add 



The rendered web part will look like this.


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.


  1. February 12, 2013 at 3:29 pm

    I really believe this amazing blog post , “Walkthrough 1: Using
    LINQ to SharePoint from within a Visual web part
    Your resource quota is 300 server resources”, especially entertaining not to mention the blog post ended up
    being a good read. Thanks,Magaret

  1. No trackbacks yet.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: