0
votes

We're working on a project that uses Linq to SharePoint. The list had several columns. After using SPMetal to make the class, it was imported into VS to access the data context. The Linq queries worked fine.

We went in a different direction, by deleting the list columns and using content types with site columns. OOTB, the add/edit forms work fine. But after updating the class with SPMetal and importing the class into VS for the data context, all the Linq query show as errors. Visual Studio cannot recognize the columns any longer because they don't appear to be there in the data context from the updated class. The columns are in the content types now, instead of the list.

Is there a way to get the content type's columns to export in the class file with SPMetal? Is there another library to import to write Linq to SharePoint queries with lists that have content types? How do you write Linq queries that use content type columns?

1

1 Answers

0
votes

So say you have a list called Documents. It has two columns called 'One' and 'Two'. You make your Linq to SP queries just fine:

DataContext dc = new DataContext("http://sharepoint");
var varResults = (from item in dc.Documents
                  where item.Two == "blah"
                  orderby item.One descending
                  select item);

Then you decide you want to use content types with site columns. The above query breaks when you delete columns 'One' and 'Two' from the list. You make site columns and assign them to a content type called 'Master', parent being item. Master has two content types deriving from it called 'CloneA' and 'CloneB'. Since the clone content type's parent is Master, then they automatically get it's site columns. When you assign the content types to the list, the definition looks like:

Column - Content types
Title - Documents, Master, CloneA, CloneB
One - Master, CloneA, CloneB
Two - Master, CloneA, CloneB

The clone content types will later be used for different Information Policies for retention on the Documents list. After breaking the inheritance and setting up the retention policies on the content types, now items can individually set to a content type which will cause the retention (1 day - CloneA, 1 week - CloneB) to kick off.

But the linq to SP queries are still broken. Even though the site columns show up, SPMetal only captures the bases content type for some reason. So to linq, the columns are not really there with the above query. Typing "where item." the 'Two' doesn't even show up. You have to cast it to make it work (probably not explaining it right). So here's the working code:

DataContext dc = new DataContext("http://sharepoint");
var varResults = (from item in dc.Documents.OfType<Master>()
                  where item.Two == "blah"
                  orderby item.One descending
                  select item);

You may be tempted to use

var varResults = (from item in dc.Documents.OfType<DocumentsMaster>()

Unfortunately, that will only return the items that are associated with that content type in the list. So if you want to get items of a certain content type to filter, knock yourself out.