Share via


How to: Manually Generate LINQ to SharePoint Entity Classes by Using SPMetal

Overview

Microsoft® SharePoint® 2010 gives developers the ability to use Language Integrated Query (LINQ) with SharePoint lists. This allows developers to use strongly-typed entities to create, edit, view, and delete SharePoint list and library data on a SharePoint site. To use LINQ with SharePoint, you must first create entity classes to represent the lists on your SharePoint site. SharePoint 2010 includes a command-line tool named SPMetal that you can use to automatically generate the data context and entity classes for the SharePoint site

Note

Note: For an example of a project that uses LINQ to SharePoint classes that were generated by SPMetal, see the SharePoint List Reference Implementation.

Summary of Steps

This how-to topic includes the following steps:

  • Step 1: Use SPMetal to Manually Create the SPLinq DataContext and Entity Class. In this step, you use SPMetal to create the data context and entity class that LINQ will use to connect to your SharePoint site.
  • Step 2: Add the Data Context and Entity Class to Your Project. In this step, you use the Microsoft® Visual Studio® development system to add the data context and entity class that you created in step 1 to your project.

Step 1: Use SPMetal to Manually Create the SPLinq Data Context and Entity Class

To create an SPLinq DataContext for an existing SharePoint site

  1. Open a Command Prompt window.

  2. Change the current path to the SPMetal.exe file location (it is usually [DRIVE:]\Program Files\Common Files\Microsoft Shared\web server extensions\14\BIN).

  3. Type the following command:

    SPMetal /web:http://MyServer/SiteName /code:SiteName.cs

    where:

    • http://MyServer/SiteName is the fully-qualified URL of your site.
    • SiteName.cs is the output file for the generated code.

    The following illustration is an example of this command.

    Ff798329.1988e9e4-9b0e-4dae-aa07-f7ed13dde217(en-us,PandP.10).png

Step 2: Add the Data Context and Entity Class to Your Project

To create an SPLinq DataContext for an existing SharePoint site

  1. Start the Microsoft Visual Studio® development system.
  2. Add the file to your Visual Studio project by right-clicking the project file and then selecting Add Existing Item.
  3. Browse to the SPMetal source directory, find your newly generated class, and then select it. Click OK to add the class to your project.

Note

For information about configuring SPMetal to automatically generate the classes as a pre-build step, see How to: Use SPMetal on MSDN.