Oct 19, 2009

Early look at LINQ to SharePoint

This post is preliminary and is subject to change, but should give you at least an idea. It refers to build 14.0.4006.1010.

In SharePoint 2007 you have to user CAML to query SharePoint lists. Anybody who have already written CAML queries knows that this can be tedious with complex queries. To make life easier the community had developed various helper tools like the U2U CAML Query Builder, YACAMLQT (Yet Another CAML Query Tool), CAML.NET… Meanwhile .NET 3.5 has been released with a cool new feature called LINQ (Language Integrated Queries). LINQ allows you to query in an object-oriented way, supports compile-time check, gives you intellisense support in Visual Studio and defines a unified, SQL like syntax to query any data source.
var result = from c in Contacts 
where c.FirstName == "Christian"
select c
The abstraction to allow querying any data source has been implemented through an interface called IQueryable(T), but don’t want to go deep in LINQ. Since now Microsoft has been delivered four LINQ providers:
  • LINQ to Object
  • LINQ to XML
  • LINQ to SQL
  • LINQ to Entity
With SharePoint 2010 the fifth LINQ Provider will be available:

LINQ to SharePoint

To don’t overlook the community efforts, there is already a LINQ provider for SharePoint on CodePlex, but since now it is in an alpha stadium.
LINQ to SharePoint works similar to LINQ to SQL or LINQ to Entity. You write business objects (or maybe a DAOs) and decorate them with mapping attributes. When the query will be executed the LINQ statements will be translated to CAML.image
The example class Contact maps to a custom Content Type with the name MyContact and the columns ID and FirstName. Writing this manual is cumbersome, therefore there’s a command line tool called SPMetal, which generates this classes for a whole site. E.g: 
SPMetal /web:http://localhost/anysite /namespace:iLoveSharePoint /code:SPDataContext.cs

Now we want to query our SharePoint list with LINQ. This is done by a class called Microsoft.SharePoint.Linq.DataContext. The DataContext represents a whole web site. SPMetal additionally generates a derived DataContext containing all lists as Properties.

Adding, editing and deleting Items with LINQ to SharePoint

Certainly you can add, edit and delete items with LINQ to SharePoint. To enable change tracking you must (not really all) implement some interfaces in your class:

  • Microsoft.SharePoint.Linq.ITrackEntityState
  • Microsoft.SharePoint.Linq.ITrackOriginalValues
  • System.ComponentModel.INotifyPropertyChanged
  • System.ComponentModel.INotifyPropertyChanging
Don’t panic, SPMetal does the job for you. I recommend to use SPMetal to generate the classes.

Let’s add new contact:

The method SumbitChanges of the DataContext persist all changes (add, edit, delete) to the SharePoint Content DB.

Relations in LINQ to SharePoint 

Another hot topic in SharePoint are relations between lists. In SharePoint 2007 CAML hasn’t any support for joins! This have made it almost impossible to build data centric applications for SharePoint 2007 (among other restrictions). But many, many customer want do that! Now with LINQ to SharePoint comes support for relational queries – Yippee!
Now we extend our sample Content Type with a lookup column to another Content Type called MyCountry and map the relation.


To map a multi Lookup you have to use EntitySet<T> instead of EntityRef<T> and set the MultivalueType property of the Association attribute to Multi. The loading of the relation is deferred. The Country object of the Contact will be loaded from the DB when the property will accessed and not before.
Get all contacts from Germany:

Yet I haven’t verified that this relational query really ends up in a SQL joins If not, this would really be bad. I will verify that as soon as possible…

I think I stop here, because things could change in further builds…


Anonymous said...

One other require stands out as the longines replica See. This may be a jewelry plated, 5 microns chrome steel condition by using a creme tinted synthetic leather bracelet. Any analog indicate regarding bright white switch makes it simple to know the amount of time with regards to crescent silent celestial body together with famous actors over the skin grant it all a truly different appearance. Any gucci replica may be a personally winding utilizing semi-automatic or fully automatic exercise together with water-repellent as many as 50 yards. Whilst a bit of highly-priced, it's together funky together with beautiful all at once. Start on, immerse themselves within the billiards using an louis vuitton replica Semi-automatic or fully automatic FFD0E001B Crescent Silent celestial body Semi-automatic or fully automatic for your wrist and also scalps transform. A good wrist watch shows significantly related to an individual's temperament together with gal favors your girlfriend to generally be dependable and even carry out a good requirement unique i . d. Identifying the have, any breitling replica watches Provider brings about the best together with attractive variations during semi-automatic or fully automatic pieces. may not be mainly beautiful; they're just trendsetters meant for different varieties the fact that get any adolescent iteration.

digital signature said...

Very helpful article ! I was always curious about all these complex algorithms that are being used in these ssl encryptions.

ahmed koutb said...

تنظيف خزانات
شركة تنظيف خزانات بالرياض
تنظيف خزانات بالرياض
تنظيف خزانات
شركة تنظيف خزانات
تنظيف خزانات

Hug Day said...

happy hug day 2016
happy hug day 2016 Date SMS Messages Quotes
How Hug Day Is Celebrated
Happy hug day sms

风骚达哥 said...

20160423 junda
air jordans
cheap oakley sunglasses
kate spade outlet
adidas stan smith
ray ban sunglasses
michael kors outlet
nike air max shoes
cheap omega watches
yeezy boost 350
michael kors outlet online
michael kors outlet online
nike free runs
air jordan uk
michael kors outlet online
pandora charms
ray ban outlet
calvin klein outlet
fitflops sale clearance
toms outlet
nike huarache white
sac longchamp
toms outlet
michael kors outlet clearance
bottega veneta outlet
armani watches
gucci handbags
nike blazer
oakley sunglasses
true religion jeans
cheap jordans
ray bans
bottega veneta handbags
jimmy choo outlet
oakley sunglasses
prada outlet
reebok shoes
true religion
burberry outlet