Click here to Skip to main content
13,292,205 members (57,570 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


5 bookmarked
Posted 10 Feb 2011

How to use LINQ to Sharepoint

, 10 Feb 2011
Rate this:
Please Sign up or sign in to vote.
How to use LINQ to Sharepoint

LINQ or Language Integrated Query is a very useful language which reduces the complexity of accessing and integrating information that is not natively defined using OO technologies, with LINQ it adds native data querying capabilities to .NET languages which makes a developer's life really easy. Since it was launched, I started using it as it saves me a lot of time in coding my applications.

Now a part of what I am doing is developing custom web parts for Sharepoint and sometimes you need to access data to present that information to your users and it would be good if LINQ can be used to query what you want to make life easier. Well that is possible and can be implemented really easily. All you have to do is reference LINQ library from your project and generate entity classes, which provide an object oriented interface to the Microsoft SharePoint Foundation content databases which can be done by using spmetal.

Now, I will give you a step by step example on how to achieve this.

First, definitely you need to have your list to play with, so in this example, I will have Employees, Department and Position. I guess it’s logical how you would relate these lists together.

For the Employees list, we have the following columns shown in the image below, take note that the Last Name column is the Title column which was renamed. Position is a lookup in the Positions list.

For Position, same thing we renamed title to Position Title and Department is a Lookup in: Department list.

For Department, again same thing, we renamed title to Department Name.

Now you have the list we need, and all we need to do next is to generate the Entity reference code using SPMetal. You can find it in the installation of your Sharepoint in the folder C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\BIN. Now to generate the Entities you need, you can run this command:

spmetal.exe /web:http://YourSharepointURL/YourTeamSite 
/namespace:YourProjectNamespace /code:FileNameYouWant.cs

Once it’s finished, you will see the file generated in the bin folder of Sharepoint. And now, you can add that to your project.

Now, you also need to add a reference to Microsoft.SharePoint.Linq.dll which is in C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI folder.

Now you are ready to code. For this sample, we will use a GridView to show the data you need, so here is a sample.

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"

        <asp:BoundField DataField="Title" HeaderText="Last Name"/>
        <asp:BoundField DataField="FirstName" HeaderText="First Name"/>
        <asp:BoundField DataField="Position" HeaderText="Position" />
        <asp:BoundField DataField="PositionDescription"

            HeaderText="Position Description" />
        <asp:BoundField DataField="Department" HeaderText="Department" />

And for the code behind, we use this:

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Linq;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Linq;

namespace Demo.VisualWebPart1
    public partial class VisualWebPart1UserControl : UserControl
        protected void Page_Load(object sender, EventArgs e)
            if (!IsPostBack)

        private void BindGrid()
            SPLinqDataContext dc = new SPLinqDataContext(SPContext.Current.Web.Url);
            EntityList<EmployeesItem> Employees = dc.GetList<EmployeesItem>("Employees");

            var EmployeeQuery = from e in Employees.ToList()
                                select new
                                    Position = e.Position.Title,
                                    PositionDescription = e.Position.Description,
                                    Department = e.Position.Department.Title
            GridView1.DataSource = EmployeeQuery;

If you notice, we use .Title a lot, this is because when we reused and renamed the Title column that SharePoint defaulted to, Sharepoint still retains the name internally it is still referred to as Title instead of the renamed column name. And if you notice because of LINQ it's easy to drill down to the sub lists that the primary list is related to, so rather than creating a join to the other list, we just use this for example:


also you notice there is a lot of Title Columns, it's easy to create an alias for it by doing this:

Department = e.Position.Department.Title

and finally if you have been using LINQ for some time, you might ask why we used:


rather than just using:


Then, here is a really good explanation about it ->

Happy coding!


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Raymund Macaalay
Technical Lead
New Zealand New Zealand

You may also be interested in...


Comments and Discussions

GeneralMy vote of 1 Pin
Member 1084957218-Jan-15 22:23
memberMember 1084957218-Jan-15 22:23 
QuestionHow to insert an Employee? Pin
Member 1068286519-Mar-14 5:38
memberMember 1068286519-Mar-14 5:38 
GeneralMy vote of 3 Pin
Mark Nischalke13-Apr-11 17:19
mvpMark Nischalke13-Apr-11 17:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171207.1 | Last Updated 10 Feb 2011
Article Copyright 2011 by Raymund Macaalay
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid