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 http://msdn.microsoft.com/en-us/library/ee537010%28office.14%29.aspx. 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

 image

 

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 http://msdn.microsoft.com/en-us/library/ee535056%28office.14%29.aspx

Sample SPMETAL Parameters XML File

image

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

myParam.xml

image

image

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

image

image

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

Nandri(Thanks)

SreenivasaRagavan

4 comments:

LOVE遊戲 said...

GREAT........................................

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 http://blog.sharepointsite.co.uk/2010/11/extending-spmetal.html

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.

http://blog.sharepointsite.co.uk/2011/05/splinq-parameterxml-to-show-hidden.html