LINQ to SharePoint - Part III

By DimitriC at September 04, 2007 22:28
Filed Under: LINQ To SharePoint

SPMetal

SPMetal is included in the LINQ to SharePoint sources-package (downloadable from the “Releases”-tab at http://www.codeplex.com/LINQtoSharePoint ). After compiling the project, you can use this application from the command line. This application takes care of generating the code (spml-files, C#-files and Visual Basic.NET-files). You can use this program directly to generate the code you want. Here you can specify if you want the spml-code or just the code (C# or VB.NET). If you execute the program you get a list of all the parameters you can set and even some examples are included.

>SPMetal.exe

Bart De Smet SpMetal SharePoint List Definition Export version 0.2.3.0
Copyright (C) Bart De Smet 2007. All rights reserved.

No inputs specified

Usage: SPMetal.exe [options]

Options:
  -url:<url>            URL to the root of the SharePoint site
  -user:<user>          User name for connection to SharePoint site
  -password:<password>  Password for connection to SharePoint site
  -domain:<domain>      Domain for connection to SharePoint site
  -list:<list>          Name of the list to export (* = all lists)
  -context:<context>    Name of the context to create
  -in:<file>            Input file with SPML for code generation
  -xml:<file>           Output file for SPML generation
  -code:<file>          Output file for code generation (= -out:<file>)
  -language:<lang>      Code language used for output: VB or CS (default)
  -namespace:<ns>       Namespace to put generated code in
  -pluralize            Auto-(de)pluralize list entity names

Syntax:
  SPMetal.exe [{online}|{offline}]
  {online}  := -url:<url> -list:<list> [-context:<context>] {connect} {option}
  {offline} := -in:<file> {codegen}
  {connect} := [-user:<user> -password:<password> [-domain:<domain>]]
  {option}  := [{codegen}|{export}]
  {codegen} := [-code:<file>] [-language:<lang>] [-namespace:<ns>] [-pluralize]
  {export}  := [-xml:<file>]

Samples:

To export one list to code (online codegen).
  SPMetal.exe -url:http://wss -list:Products -language:CS -code:Products.cs

To generate an SPML mapping definition for all lists (online export).
  SPMetal.exe -url:http://wss3demo -list:* -xml:Northwind.dbml

To generate code for an SPML mapping definition (offline codegen).
  SPMetal.exe -in:Northwind.spml -language:CS -code:Northwind.cs


If you only want the code, which forms the bridge between your application and the SharePoint site, you can generate a cs- or vb-file by setting the language parameter to respectively CS or VB. Let’s say I want only the C#-code for my example application, I would execute SPMetal with the following parameters:

>SpMetal.exe -url:http://fileserver -list:books -language:CS -code:Books.cs -user:dimitri -password:LinqToSharePoint

This will give the following output:

Bart De Smet SpMetal SharePoint List Definition Export version 0.2.3.0
Copyright (C) Bart De Smet 2007. All rights reserved.

Output written to Books.cs.

The file has been generated in the \bin\Debug\ folder of the SPMetal-project. Now you need to import this file into your project in Visual Studio 2008. Just drag the file to your project (in the Solution Explorer) and it will be added immediately.

Using this file goes a little different than using an spml-file. First you must instantiate a SharePointDataContext, which will connect to the SharePoint site. Again, if you have to use a login and password, add the “using System.Net;”-directive to your project as you will need to create NetworkCredentials and attach them to the connection so queries can be executed.

var ctx = new SharePointDataContext(new Uri(http://fileserver));
ctx.Credentials = new NetworkCredential("dimitri","LinqToSharePoint");           

var books = ctx.GetList<Books>();        

var res = from b in books
          select new { b.Title, b.Author};        

 foreach (var u in res)
       Console.WriteLine(u);

Executing this code will give you a list of all the books in the list by title and author.

{ Title = Common Language Runtime, Author = Steven Pratschner }
{ Title = Programming .Net Components, Author = Juval Löwy }
{ Title = Design Patterns in C#, Author = Steven John Metsker }
{ Title = CLR via C#, Author = Jeffrey Richter  }
{ Title = Professional ASP.NET 2.0, Author = Bill Evjen, Scott Hanselman, Farhan Muhammad, Devin Rader, Srinivasa Sivakumar }
{ Title = Patterns of Enterprise Application Architecture , Author = Martin Fowler }
{ Title = Windows Presentation Foundation Unleashed , Author = Adam Nathan }
{ Title = Managing Software Requirements, Author = Dean Leffingwell, Don Widrig }
{ Title = Microsoft SharePoint 2007 Unleashed , Author = Michael Noel, Colin Spence }

You can also use the context-switch to create your own SharePointDataContext. This will then have a property which you can use to perform queries. The –context property is set like any other SPMetal property:

>SpMetal.exe -url:http://fileserver -list:books -language:CS -code:Books.cs -user:dimitri -password:LinqToSharePoint –context:MyContext

When you use this code-file in your project, you can do this:

MyContextSharePointDataContext src;

Now you will be able to use the books-property: src.books.

For generating spml-files like you can do in Visual Studio 2008 (after installing LINQ to SharePoint), use SPMetal with the following parameters:

>SPMetal.exe -url:http://fileserver -user:dimitri -password: LinqToSharePoint -list:books -xml:books.dbml

You can open this dbml-file in a text editor and see the generated XML. This is the XML description of the selected list on the SharePoint site. Now, based on this description, you can SPMetal generate the mapping definition ( = offline code generation). This code is exactly the same as when you use online code generation (meaning, with a connection to the server as explained in the first part of this chapter).
Comments are closed