Blog Home  Home |  Breeze Home RSS 2.0 Atom 1.0 CDF  
Mick's Breeze Blogs - Biztalk/Sharepoint/... - SharePoint 2010: SPMetal.exe and system fields namely ContentType
Things hard and not so hard....
# Wednesday, July 28, 2010

Olaf and I were cracking away on some SharePoint 2010 work which we thought should be simple…point SPMetal to the site and start LINQ-ing to our hearts content…..

with the one exception that we couldn’t select items from a list based on their Content Type.

By default SPMetal.exe doesn’t include these ‘system’ fields (apart from ID + Title – go figure) and the secret is to use an Override file.

The good oil is:http://msdn.microsoft.com/en-us/library/ee535056.aspx
(Here’s a good article on how .NET Types are mapped to SharePoint - http://msdn.microsoft.com/en-us/library/ee536245.aspx)

The simple override/parameters file:

<Web AccessModifier="Internal" xmlns="http://schemas.microsoft.com/SharePoint/2009/spmetal">
  <ContentType Name="Item" Class="Item">
    <Column Name="ContentType" Member="ContentType" />
  
  </ContentType>
 
</Web>

 

The SPMetal Command Line

image

The VS.NET Code

 static void Main(string[] args)
        {
            using (BreezeDataContext dc = new BreezeDataContext("http://breezelocal"))
            {
                var myitems = from i in dc.GetList<ContentListTraining>("My Content List")
                              where i.ContentType == "Training"
                              select i;
                var courses = myitems.ToList<ContentListTraining>();

                Console.WriteLine("There are {0} items",courses[0].Title);
                   
            }

            Console.ReadLine();
        }
Wednesday, July 28, 2010 6:10:19 PM (AUS Eastern Standard Time, UTC+10:00)  #    Comments [0]   .NET Developer | SharePoint | 2010 | Tips  | 
Copyright © 2010 Breeze Training. All rights reserved.