Tuesday, November 3, 2009

SPMetal Linq with SharePoint 2010

LINQ and SharePoint 2010

SPMetal is a command line tool that generates entity classes, which provide an object oriented interface to the Microsoft SharePoint Foundation content databases. These classes are primarily used in LINQ to SharePoint queries; but they are also be used to add, delete, and change list items with protection against concurrency conflicts. Finally, they can be used as an alternative to the regular SharePoint Foundation object model for referencing content.

The tool is included with SharePoint Foundation and is usually located in %ProgramFiles%\Common Files\Microsoft Shared\web server extensions\14\\BIN.

SPMetal is typically executed as a prebuild command in Microsoft Visual Studio because the code it generates is usually part of a solution that includes a new site type that is itself evolving during the development cycle. Because SPMetal overwrites the files it generates with each execution, for a given output path and against a given Web site, you should not make changes to these files. The classes it generates are partial, so you can add members to them in separate, manually created, code files.

If your solution is based on an existing site type, such as the Team Site that ships with SharePoint Foundation, and it references only lists that are always present in such sites and only fields (columns) that are always present in those lists, you can run SPMetal just once and make changes to the generated file that will not be overwritten.

For more information about the practicalities of using SPMetal, see How to: Use SPMetal.

SPMetal uses the basic syntax of other Windows command line tools:

SPMetal [options]

Each option is preceded by a forward slash. Most options require values, in which case the option name is followed by a colon and the value follows the colon:

SPMetal /option1 /option2:value /option3:"value with a space"

Options can be in any order. Not every option is required, but if you include an option, you must give it a value. If a value contains spaces, you must enclose the whole value in quotation marks (").

Example
Generate C# code:
SPMetal /web:http://ContosoServer/Marketing /code:MarketingSite.cs

Generate Visual Basic code for client-side execution with custom parameters settings:
SPMetal /web:http://ContosoServer/Marketing /code:MarketingSite.vb /useremoteapi /parameters:MarketingSite.xml

Generate Visual Basic code from a specified user context:
SPMetal /web:http://ContosoServer/Marketing /code:MarketingSite.vb /user:Contoso\sally /password:7Yj38d

2 comments:

  1. I have heard about another way of sql server 2000 recovery database. Besides, you can visit my blogs at: http://daspeac.livejournal.com/ or http://daspeac.blogspot.com/ where I’m trying to share my experience with regard to data corruption issues

    ReplyDelete
  2. You can also use the new LINQ to SharePoint DSL Extension for Visual Studio 2010 available here:
    http://code.msdn.microsoft.com/linq2spdsl/

    This tool is useful to control SPMETAL code generation thanks to a schema inside Visual Studio, like LINQ to Entities.

    ReplyDelete