SPMetal is a tool provided by SharePoint that allows you to use LINQ to query your SharePoint lists and libraries (goodbye CAML)! I love SPMetal because it allows me to use strongly typed classes rather than parsing through XML.

I did notice, however, in my latest endeavor that SPMetal has some limitations, one of which is that it does not make all fields available. Many of the private fields are missing, such as Created, CreatedBy, Modified, ModifiedBy, and Item Link. There are two ways to fix this:

1) You can modify the file SPMetal generates to add these fields for yourself, which is super easy… until you need to regenerate the SPMetal file.

~~ OR ~~

2) You can override the SPMetal Defaults by using a parameters file. (This seems like the better solution, only it doesn’t work for me.  If you can get this to work, please let me know!)

How do I create an SPMetal file to include in my project?

I typically run this from the Desktop of my development SharePoint server. If you run it elsewhere, you will need to update the path to the file you create.

1. Create a text file with a .BAT extension and name it SPMetal.bat

2. Right-click on the file and choose Edit.

3. Add the following text.

cd c:\program files\common files\microsoft shared\web server extensions\15\bin
spmetal.exe /web:http://dev.mydevsite.com /code:”c:\SPMetal\SPMetal.cs”
pause

4. You will need to put in the url to your SharePoint site and specify a path you want to save the SPMetal generated class file to.

5. Save and close the file.

6. Double click the file to run the batch file. It will traverse your SharePoint site and create class entities for your lists and libraries.

7. Add the SPMetal.cs class library to your SharePoint project. You will need to reference one additional DLL, so that it will compile: Microsoft.SharePoint.Linq, which can be found at c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Linq.dll

Modify the file SPMetal generates to add these fields for yourself

Go into the SPMetal .cs file created by the SPMetal command and locate the Item class.

publicpartialclassItem : Microsoft.SharePoint.Linq.ITrackEntityState

Add the following code to the item class for each field you want to make available. In this example, I have added the Created, Created By, Modified, Modified By, and List Item link fields.

string _CreatedBy;
string _ModifiedBy;
DateTime _Created;
DateTime _Modified;
string _ServerUrl;

// this is the list item link field
[Microsoft.SharePoint.Linq.ColumnAttribute(Name = “ServerUrl”, Storage = “_ServerUrl”, ReadOnly = true, FieldType = “String”, IsLookupValue = false)]
public string ServerUrl {
get { return this._ServerUrl; }
set {
if ((value != this._ServerUrl)) {
this.OnPropertyChanging(“ServerUrl”, this._ServerUrl);
this._ServerUrl= value;
this.OnPropertyChanged(“ServerUrl”);
}} }

// this is the created by field
[Microsoft.SharePoint.Linq.ColumnAttribute(Name = “Author”, Storage = “_CreatedBy”, ReadOnly = true, FieldType = “User”, IsLookupValue = true)]
public string CreatedBy {
get { return this._CreatedBy; }
set {
if ((value != this._CreatedBy)) {
this.OnPropertyChanging(“Author“, this._CreatedBy);
this._CreatedBy= value;
this.OnPropertyChanged(“Author“);
}} }

// this is the modified by field
[Microsoft.SharePoint.Linq.ColumnAttribute(Name = “Editor”, Storage = “_ModifiedBy”, ReadOnly = true, FieldType = “User”, IsLookupValue = true)]
public string ModifiedBy {
get { return this._ModifiedBy; }
set {
if ((value != this._ModifiedBy)) {
this.OnPropertyChanging(“Editor“, this._ModifiedBy);
this._ModifiedBy= value;
this.OnPropertyChanged(“Editor“);
}} }

// this is the create date field
[Microsoft.SharePoint.Linq.ColumnAttribute(Name = “Created”, Storage = “_Created”, ReadOnly = true, FieldType = “DateTime”, IsLookupValue = false)]
public DateTime Created {
get { return this._Created; }
set {
if ((value != this._Created)) {
this.OnPropertyChanging(“Created“, this._Created);
this._Created= value;
this.OnPropertyChanged(“Created“);
}} }

// this is the modified date field
[Microsoft.SharePoint.Linq.ColumnAttribute(Name = “Modified”, Storage = “_Modified”, ReadOnly = true, FieldType = “DateTime”, IsLookupValue = false)]
public DateTime Modified {
get { return this._Modified; }
set {
if ((value != this._Modified)) {
this.OnPropertyChanging(“Modified“, this._Modified);
this._Modified = value;
this.OnPropertyChanged(“Modified“);
}} }

Override SPMetal Default with a parameters file

This one would be my preferred method, but I can’t seem to get to work.  If you get this to work, please let me know!  Here are some references:

Overriding SPMetal Defaults by Using a Parameters XML File

Using SPMetal with Parameters

Advertisements