LINQ to SharePoint is one the most awaited feature that is added in SharePoint 2010 . Before you can start writing LINQ queries you need to use a command line tool called “SPMETAL” that generats the entity classes. There are number of parameters that can be used while running this query , but some of most common one are “web , namepsace and code”. By the way if you are working exclusively with Visual Studio 2010 , and want to get entities source code generated from inside Visual Studio 2010 , you may want to use a extension developed by a fellow MVP Waldek, you can read and download it from here.
To get started , I created a simplest Window Form application that will generate the code file using SPMETAL and will open the file in notepad , so code can be copied. I will come back to the ideal scenario later but first lets go though and use this simple UI (called SPMETALEntityGen). At this moment it is bit of quick and dirty way of doing it , I will try to perform some optimizations in the future.
1. In “Path” field change the drive letter to the one where SharePoint is installed . Chances are “C:” will be the drive where it is installed.
2. Fill “SiteUrl”, “namespace” and “code” fields.In the code field you can use both the full path like “c:mycodefile.cs” or simply “”mycodefile.cs” , in the second case file will be created inside where “SPMETAL” tool is located , which is default to “BIN” folder inside the SharePoint “14” hive , or simply put it will be right inside “Path” the path field.
3. Click on the “Run” button . You will be shown a dialog which will give you option to open the code file. If you select “Yes” , it will open the file in “notepad”. Also note that in case the file was already exist , you can also select to either go ahead and override the existing file or don’t continue with code file generation. Both of these dialogs are shown below.
You can download the tool along with source code from here.