SharePoint 2010 Makes the Link to LINQ

  • March 1, 2010
  • By Razi bin Rais
  • More Articles »
LINQ (or Language Integrated Query) has been extensively used by the developer community in the last couple of years. Its rich semantics, compile-type checking, along with "intelliSense" support in Visual Studio, offer many advantages for developers.

SharePoint developers, however, did not get to enjoy LINQ for the most part, because there was no support for it in SharePoint 2007. (There were some efforts from community members to enable using LINQ with SharePoint 2007 by using custom-made tools.)

But wait no more, because LINQ to SharePoint finally arrived in SharePoint 2010. As expected, it provides the running of LINQ queries over the SharePoint 2010 Lists / Libraries. Internally, LINQ to SharePoint generates the laborious CAML queries; this takes the burden of writing complex CAML queries away from the developer, so his or her time can better spend on other productive tasks. However, please note that CAML is still fully supported in SharePoint 2010, and in certain scenarios it may be better fit than LINQ.

This article will show you how to use tool a called "SPMETAL" to generate the source code file for LINQ to SharePoint, then use it to query the SharePoint List, and finally insert the item in the List. For an example, a create a SharePoint "Custom List" called "Customers." You can give custom lists any name, just make sure you end up having two custom columns named "Name" and "Address," both of type "single line of text." Also add an item to the list. The following image shows the list item along with columns.

The new utility called "SPMETAL.exe" will be used to generate the source code file that will be used to perform LINQ to SharePoint queries. Perform the following steps to generate the source file.

1. "SPMETAL.exe" is located in "BIN" inside the SharePoint installation directory. "<Drive-Letter>:\Program Files\Common Files\Microsoft Shared\Web Service Extensions\14\BIN". Where "<Drive-Letter>" is the drive where SharePoint is installed.

2. Execute the "SPMETAL" command, along with parameters as shown below.

The "web" targets the website containing the lists and libraries. "Namespace" is the namespace used in the generated code, and "code" is the name of the code file. At this moment, code file can be either "C#" or VB.NET. You may want to change the "web" parameter from "http://win2k8-r2-01" to website where you had created the "Customer" list.

3. The code file "SharePoint.cs" will be generated inside the "BIN" folder. In case you want to change the path where the source file should be generated, use absolute path e.g "c:SharePoint.cs".

4. The source file is ready. In order to utilize it, create a new "Windows Console Application" project in Visual Studio 2010, use "Visual C#" because the source code generated by "SPMETAL" was in C# (in case you want to work with VB.NET just change the ".cs" to ".vb" in step 2, this will result in source file for VB.NET. Technically speaking, Visual Studio 2010 is not mandatory. The only thing required is an application that can leverage the code file to perform LINQ to SharePoint queries. However the requirement that the application must be compiled against "x64" still stands, if the target platform is different, SharePoint will not be accessible. Once the project is created, add the source file to the project. The following image shows the project after the source file "SharePoint.cs" is added.

12


Networking Solutions







Partners