SPMetal – Missing Created By/Modified By fields
Posted
Friday, February 19, 2010 10:50 PM
by
Itay Shakury
Update: I have posted a more appropriate solution for this problem here: http://blogs.microsoft.co.il/blogs/itaysk/archive/2010/09/22/spmetal-default-fields.aspx
I have been playing around with Linq 2 SharePoint (SharePoint 2010 beta 2), and noticed that SPMETAL fails to generate the appropriate fields for the ‘Created By’ and ‘Modified By’ fields.
This is probably a bug with SPMETAL, but here’s a quick workaround.
generate a code file with SPMETAL as usual, and look for the ‘Item’ class.
Add this piece of code inside the class:
string _CreatedBy;
[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("CreatedBy", this._CreatedBy);
this._CreatedBy = value;
this.OnPropertyChanged("CreatedBy");
}
}
}
Notes:
I have created this as part of a ‘User’ field type (It might be useful sometime), but you can do the same with plain string. If you do, the value of the property will include both the ID and the string part if the User lookup.
I have omitted the Id property (which is supposed to be part of a ‘Lookup’ field declaration) because it is useless in this case.
If you would like to do the same for ‘Modified By’ field, copy the same code, and replace “Author” with “Editor” in the ‘Name’ property of the ‘ColumnAttribute’.