Partilhar via


SPMetal automation in the LINQ to SharePoint DSL Extension

Hi,

I added some weeks ago a new feature in the LINQ to SharePoint DSL Extension to embed the SPMetal classes in Visual Studio. The idea of this tool is to add two new things in Visual Studio 2010:

  1. Generation of a SPMETAL.EXE parameter XML file automatically from a LINQ to SharePoint Data Model (l2sp file)
  2. Automatic call to the SPMETAL.EXE inside Visual Studio in the build panel

NOTE: If you don't know, SPMETAL.EXE is a binary tool distributed with SharePoint 2010 to automatically generate the LINQ to SharePoint entities of an existing SharePoint 2010 web site. 

In fact, you can use LINQ to SharePoint DSL Extension without SPMETAL because you can already generate the entity classes thanks to the automatic T4 builder (the custom tool attached on your l2sp files). But this classes generator is not the official one, offically supported by Microsoft. For this reason, I prefer to leave to the user the choice between the automatic classes generation and the SPMETAL experience in Visual Studio.

To use this feature, you only need to use the SPMetal items in the context menu. You can find it by right clicking everywhere in the schema in design mode or on your l2sp file in the solution explorer :

 

If you click on the "Launch SPMETAL classes generation" item, the add-in will first generate to parameter XML file from your model and after that call SPMETAL directy in your Visual Studio build panel:

 

 If the generation is successful, you find new files under your l2sp file in the solution explorer:

The *.l2sp.cs file contains the entity code generated by SPMETAL and the *.l2sp.spmetal.xml file contains the generated SPMETAL parameter file.

This parameter file contains the naming convention defined in your LINQ to SharePoint data model (l2sp file) to force SPMETAL to generate the entities with the names that you defined.

The *.spmetal.xml looks like it:

<?xml version="1.0" encoding="utf-8"?>

<Web xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="https://www.w3.org/2001/XMLSchema" AccessModifier="Public" Class="MySharePointSite3" xmlns="https://schemas.microsoft.com/SharePoint/2009/spmetal">

  <List Name="Links" Member="LinksItem">

    <ContentType Name="Link" Class="LinksItem">

      <Column Name="URLwMenu2" Member="URL3" />

    </ContentType>

  </List>

  <ExcludeOtherLists />

  <ContentType Name="Item" Class="Item">

    <Column Name="ContentType" Member="ContentType" />

    <Column Name="Title" Member="Title" />

    <ExcludeOtherColumns />

  </ContentType>

  <ContentType Name="Document" Class="Document">

    <Column Name="Created_x0020_By" Member="DocumentCreatedBy" />

    <Column Name="Modified_x0020_By" Member="DocumentModifiedBy" />

    <Column Name="FileLeafRef" Member="Name" />

    <ExcludeOtherColumns />

  </ContentType>

  <ContentType Name="Link" Class="Link">

    <Column Name="Comments" Member="Comments" />

    <Column Name="URL" Member="URL" />

    <Column Name="URLwMenu" Member="URL1" />

    <Column Name="URLNoMenu" Member="URL2" />

    <ExcludeOtherColumns />

  </ContentType>

  <ExcludeOtherContentTypes />

</Web>

 To have more information about the format of the SPMETAL parameter file, you can consult this MSDN link:

https://msdn.microsoft.com/en-us/library/ee535056.aspx

Note that you can also modify some advanced settings on SPMETAL generation (like specify credentials, or if you want to use the remote API).

To conclude, this feature is useful to drive the SPMETAL generation directly in Visual Studio but in the same time keep the LINQ to SharePoint DSL Extension model designer to manage the naming convention of your entities.

 

IMPORTANT NOTE #1: It's important to remember that if you don't remove the existing custom tool associated with your l2sp file in VS, the SPMETAL generated code could be remove if you save your model. Read the documentation to have more information about that.

IMPORTANT NOTE #2: With the LINQ to SharePoint DSL Extension generator, you can generate entity code without a real existing web site. Don't forget that with SPMETAL, all the lists, content types and columns defined in the parameter file must exist on your web site when you're launching the generator.