SharePoint 2010: How to use LINQ to generate CAML Query

LINQ to Sharepoint is one of cool new feature in SharePoint 2010, and LINQ can be used to generate CAML Queries.

Here is how that works -

When we write a LINQ query against the list, it will get translated into CAML query by the LINQ to Sharepoint provider at the runtime. The advantage is we write simple LINQ queries, and the complexity of the CAML queries are eleminiated.
One of the prerequiste for LINQ to SharePoint provider is to generate the entiry classess of list, this manual task can be automated by using SpMetal.exe command line tool, which will generate the required entity class for us.

Note: Spmetal.exe is a command line utility, and found at the following folder %programfiles%\common files\Microsoft Shared\Web Server Extensions\14\BIN

Now how to execute spmetal.exe, there are two ways to execute the utility
Step 1: Execute SPMetal command line tool




  1. Run the SpMetal.exe command in the command line, or


  2. Use prebuild event of your visual studio 2010 environment to generate the Entity class
Here is the dump of SPMetal options available

SPMetal [options]
Options
/web: Specifies absolute URL of the web-site. Host address can be local, in which case Server OM will be used to connect to the server.
/useremoteapi Specifies that the web-site URL is remote.
/user: Specifies logon username (or domain).
/password: Specifies logon password.
/parameters: Specifies XML file with code generation parameters.
/code: Specifies output for generated code (default: console).
/language: Specifies source code language. Valid options are "csharp" and "vb" (default: inferred from source code file name).
/namespace: Specifies namespace that is used for source code (default: no namespace).
/serialization: Specifies serialization type. Valid options are "none" and "unidirectional" (default: none).

Generate C# source code from a local SharePoint site:
SPMetal /web:http://localserver:5555/localsite /namespace:nwind /code:nwind.cs

Generate VB source code from a local SharePoint site with specified parameters:
SPMetal /web:http://localserver:5555/localsite /namespace:nwind /code:nwind.vb /parameters:parameters.xml
Generate C# source code from a remote SharePoint site using default credentials:
SPMetal /web:http://remoteserver:5555/remotesite /useremoteapi /namespace:nwind /code:nwind.cs
Generate C# source code from remote SharePoint server using Client OM with specified credentials:
SPMetal /web:http://remoteserver:5555/remotesite /user:domain\username /password:password /namespace:nwind /code:nwind.cs
Let us generate a strongly typed entity class using the tool SPMetal, and here is the command line I used for creating MyEntityClass.cs

SPMetal /web:http://winbase:1000 /code:MyEntityClass.cs

if you use vb, then your command line should be as follows

SPMetal /web:http://winbase:1000 /code:MyEntityClass.vb
Step 2 : Add the generated entity class to your Visual Studio project,
Step 3: Add reference to Microsoft.SharePoint.Linq to your project and add the following using statements to your code


using System.Linq;


using Microsoft.SharePoint.Linq;





Step 4: Use the Log property of the DataContext object to generate the log


Sample source Source:


using(MyEntityClass dc = new MyEntityClass(http://winbase:1000/))


{


dc


}

Comments