Thursday, January 14, 2010

Overriding SPMETAL.EXE Default Code Generation With XML Parameters File option.

SPMETAL.EXE is command line tool which is used for to generate SharePoint Objects as Entity classes. after that you can use LINQ to SharePoint to Query Add, Delete and Update the SharePoint objects. You can find this tool in 14 hive directory where SharePoint installed  %ProgramFiles%\Common Files\Microsoft Shared\web server extensions\14\\BIN  .

For SPMETAL.EXE Defaults  code generation rules  please refer the following URL In this blog we are going to override the default code generation with XML parameters file option.

SPMETAL Command line option Syntax :

 SPMETAL.exe … /parameters:XMLParametersFileName.xml

SPMETAL Identifies the path and name of an XML file that contains overrides of SPMetal default settings. mostly we  will not reuse exactly the same parameters XML file for different Web sites, so name the file the same as the Web site . This gives us easy to identify the file with site name and make changes.

XML Parameters  File Schema



The Top level element is <Web>. Web element has two attributes 1 ) Class 2) Access Modifier.  both attributes are optional. You can set class attribute if you do not want SPMetal default name given to DataContext derived class. the same way you can set Class modifier as either internal (in C#)  or Friend (in VB.NET ) but the default generated class access modifier is public.

<List>  you add this element child to the <Web> element if you want to change SPMETAL generates a property to  a list.

please refer the following link for more details

Sample SPMETAL Parameters XML File


The above picture shows All the Lists in my site. Now i am going to generate the entity class for only Sreenicontacts List by passing XML Parameter file so that i can override default behavior of the SPMETAL.

SPMETAL /web:http://localhost:44119 /code:SreeniLinqSPByParamCtx.cs /parameters:myparam.xml




Here is the class diagram of the Entity which is generated my SPMETAL and passing the above XML Parameters File.



Using XML parameters File options we can override the Default code generation of SPMETAL.EXE.




Anonymous said...


none said...
This comment has been removed by the author.
Paul Beck said...

Nice clean explaination of the parameters file. I have refered to your post in this blog post

Paul Beck said...

I made a new post to help me display my hidden fields in SPMetal. A key takeaway is that IncludeHiddenFields won't create the property for the ContentType field.