>All You wanted to know About LINQ To SharePoint



LINQToSharePoint is very similar to LINQTOSQL. By using LINQTOSHAREPOINT you can now fetch Lists by using LINQ instead of using CAML queries.

  • Simplified Object-Oriented Way to Query
  • No CAML Required
  • SPMetal.exe Generates Entities Classes for SharePoint Lists
  • Compile Time Check unlike CAML’s RUN Time Check
  • Entity classes are strongly-typed, VS provides intellisense

Disadvantage of CAML

  • CAML was difficult to use as it is an XML Based Query Language.
  • CAML was written as STRING into SPQuery, SPSiteDataQuery


MSDN Says:

"SPMetal is a command line tool that generates entity classes, which provide an object oriented interface to the Microsoft SharePoint Foundation content databases. These classes are primarily used in LINQ to SharePoint queries; but they are also used to add, delete, and change list items with concurrency conflict resolution. Finally, they can be used as an alternative to the regular SharePoint Foundation object model for referencing content"

The tool is included with SharePoint Foundation and is located in %Program Files%\Common Files\Microsoft Shared\web server extensions\14\BIN.To Find exact syntax and Options with SPMetal please click here

How To:

Lets do quick lab session to understand LINQTOSHAREPOINT.

Step 1:

a) Create a new Contact List named "Employee" in your site

b) Create a New Column called "NetSalary", Type: Currency

c) Add some records to the Employee List

Step 2:

a) Open SharePoint 2010 Management Shell ( make sure "Run as Administrator")

b) Run below command to Generate Entities Class

spmetal /web:<<SiteURL>> /code:MySite.cs

EX: spmetal /web:http://prawal01:8080/ /code:MySiteEntities.cs

Figure 1:


c)Verify "MySiteEntities.CS" file is available in current Directory

Create a Visual Web Part where we will make use of above Entities Class to Fetch Data from Employee list.

a) Create a New Visual Web Part Project

b) Refer Microsoft .SharePoint.linq

c) Add the Entities class to you project through Add Existing Item


d) Add a Literal Control to ASCX Control

<asp:Literal ID="Display" runat="server" />

e) Add Below code to Page Load Event

StringBuilder writer = new StringBuilder();     
    using (MySiteEntitiesDataContext dc = new MySiteEntitiesDataContext(SPContext.Current.Web.Url))          
         //Query Expressions
         var q = from emp in dc.Employee                      
                 where emp.NetSalary > 5000
                 //orderby emp.Project.DueDate                       
                 select new { emp.Title, emp.FullName, emp.NetSalary};
               writer.Append("<table border=\"1\" cellpadding=\"3\" cellspacing=\"3\">"); 
             foreach (var employee in q)              
   catch (Exception x)      
      Display.Text = writer.ToString();

Now Deploy Web Part to your site.


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