A while back I created a post about SPMetal.

http://www.spdavid.com/post/2010/02/18/Using-SPMetal-to-gernerate-code-for-a-specific-List.aspx

In this post I will expand a little bit on SPMetal and write about how to use it with Visual Studio 2010 in a way that I have found effective.

Create an empty SharePoint 2010 Solution with VS 2010. (Make sure it is .NET 3.5)

  1. Create a Folder called SPMetal
  2. Add 3 files to the folder and the following contents
    • GenerateSPMetalCode.bat
      "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\BIN\SPMetal" 
      /web:http://SPDavid /namespace:SPDavid.SPMetal /code:SPMetal.cs /language:csharp 
      /parameters:SPMetal.xml 
    • SPMetal.xml

    <?xml version="1.0" encoding="utf-8"?>
    <Web AccessModifier="Internal"
    xmlns="http://schemas.microsoft.com/SharePoint/2009/spmetal">
    <List Name="Documents">
    <ContentType Name="Document" Class="Document" >
    <Column Name="Title" Member="Title" />
    <Column Name="Author" Member="CreatedBy" />
    <Column Name="Created" Member="Created" />
    </ContentType>
    </List>
    <List Name="Announcements">
    <ContentType Name="Announcement" Class="Announcement">
    <Column Name="Body" Member="Body" />
    <Column Name="Title" Member="Title" />
    <Column Name="Author" Member="CreatedBy" />
    <Column Name="Created" Member="Created" />
    </ContentType>
    </List>
    <ExcludeOtherLists></ExcludeOtherLists>
    </Web>

     

    • And an empty SPMetal.cs
  3. It should now look like the following
    image
  4. Make sure the lists exist in the site that you specified in the bat file. 
  5. Open the SPMetal directory in Windows Explorer and run the GenerateSPMetalCode.bat file.
  6. After a successful run open up the SPMetal.cs file and confirm that the code has been generated.
  7. You can now use the generated code with LINQ.