Getting started with LINQ to SharePoint
One of the major advantages of SharePoint 2010 is the support for LINQ. If you are new to LINQ, I would recommend you to read the below article from MSDN.
With the support of LINQ to SharePoint, in SharePoint 2010 you can apply LINQ queries to your SharePoint objects, a great feature that helps developers who struggle to write CAML queries. In this article I am going to demonstrate how you can do the LINQ to SharePoint. When executing the runtime will convert LINQ queries to corresponding CAML queries and then execute against SharePoint.
For the purpose of demonstration, I am going to query tasks list that comes up with almost all SharePoint site definitions. The tasks list has fields such as Title, Description, Priority, Assigned to etc. IN this demonstration I am going to list all the high priority tasks from the Tasks list. Using LINQ it is easy to extract data using the Object model. I have entered certain tasks in my task list. The data available for the tasks list is as follows.
As you can see, there are 2 high priority tasks. So What I am going to do is to list them in the user’s home page using a custom web part.
In order to access any list using LINQ to SharePoint, first you need to create the entity class for the list. SharePoint 2010 shipped with SPMetal.exe that will generate entity class for you. You can find SPMetal.exe in the following location
%ProgramFiles%\Common Files\Microsoft Shared\web server extensions\14\BIN
The 14 folder under web server extensions is referred as “14 hive” and you may find this term in the remaining part of this article and you need to translate this to the correct folder.
To read more about SPMetal, refer the following MSDN article.
To generate code file for your site, just navigate to the Bin folder under 14 hive from command prompt and execute SPMetal Command with the necessary options. I have executed SPMetal command as follows.
Once the command executed, MyCode.cs is generated in the specified location, c: in my case.
Now create a new SharePoint visual web part project. If you want to know how to create a visual web part you can refer the following article.
I have created a Visual Web part and named it as LinqTest. I have deleted the default web part and added a new visual web part with name LinqTestWebPart. In the solution explorer, the project looks as follows.
Now we need to add the entity class created early to this project. Right click the project, select add existing item then select MyCode.cs (or the name you selected for your class file). After added the generated class, the solution explorer will looks similar to following snapshot.
In order to use LINQ to SharePoint in your code, your project needs to refer the following dll.
Microsoft.SharePoint.Linq.dll
This dll can be found in the ISAPI folder under 14 hive. Right click your project, click on add reference. In the add reference dialog, select Browse tab, Browse to the ISAPI folder and select Microsoft.SharePoint.Linq.dll
Click OK so that the reference will be added to the project.
In the Visual web part user control, I placed a panel control, where I am going to display high priority tasks. The source of the usercontrol is as follows.
As you can see, there is just a panel control with ID pnlTasks.
Now we can start writing our code. Basically you need to create a data context object by passing the site url. Once you have the data context object, you can directly query the lists using the object model. I have entered the following code.
DataContext thisSite = new DataContext(SPContext.Current.Web.Url);
EntityList<Task> tasks = thisSite.GetList<Task>("Tasks");
var highPriorityTasks = from t in tasks
where t.Priority == Priority._1High
select t;
foreach (Task highPriorityTask in highPriorityTasks)
{
pnlTasks.Controls.Add(new LiteralControl("<b>" + highPriorityTask.Title + "</b><br/>"));
}
Make sure you included the following using statements in the top of the code file.
using System.Linq;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Linq;
The snap shot for the entire code is given below .
I have deployed the web part to my SharePoint farm. In the home page, I inserted the LinqTestWebpart in the page. The output of the page is as follows.
As you have seen, with couple of lines of code, we can do complex tasks. In real time you will have some data bound control such as grid view /list view and bind the results gained from LINQ to the control directly. With LINQ to SharePoint, you will get access to the lists as native objects, the possibilities for developers are seamless and you are not supposed to worry about the CAML syntax.