LINQ to SharePoint in SP2010 – Part1

Recently, i was developing a custom Web Part in SP2010. Which needs to communicate with multiple custom list. I started development using traditional CAML tool approach to communicate with Custom lists. But i found some limitations during the development and thought to use LINQ instead of CAML. So, i am trying to explain What is CAML? What are its disadvantages to use and How We can use LINQ instead.
Let me start with the CAML basics.

What is CAML?
Collaborative Application Markup Language.
CAML is an xml structured intermediate query syntax for SharePoint to query against data structures. SharePoint takes your CAML and queries against its sql backend. With CAML you have this massively verbose query syntax that requires nested xml tags, multi-line statements, and quoted attributes.

Disadvantages of CAML:
Since CAML query is text based, If you are joining two lists accross a lookup field there may be various problems.
Developer do not know until run time if the query is written correctly. If the query is not correct, then it will simply fail at run time.
It has no IntelliSense support at design time.
When writing the query, developer have no idea what CAML elements are legal in the syntax without having a reference open.
The query is difficult to read. Developer cannot determine easily what the query is doing and what lists are being joined.
The data returned from the query is placed in a SPListItem collection, which does not provide strongly typed business entities.

Limitations:
1. In SharePoint 2010, you can join the primary list to a foreign list and include more than one field from the foreign list. However, the limitation is that the included fields from foreign list have to be the following type:
• Calculated (treated as plain text)
• ContentTypeId
• Counter
• Currency
• DateTime
• Guid
• Integer
• Note (one-line only)
• Number
• Text
The above limitation also explains why you cannot include some types of the fields from the remote list when creating a lookup.

2. When using CAML query to join SharePoint lists, there can be joins to multiple lists, multiple joins to the same list, and chains of joins. However, the limitations are only inner and left outer joins are permitted and the field in the primary list must be a Lookup type field that looks up to the field in the foreign list.

3. The support for writing the JOIN query in CAML is very limited.I have to hand-code the CAML query to join the lists,not fun at all

To overcome this we have LINQ to SharePoint in SP2010. Let me describe you few advantages of LINQ in SharePoint.

Advantages of LINQ:
It is an object-oriented query language.
It provides strongly typed objects at design time, you can create queries in code and know that they are correct because the code compiles.
The strongly typed objects provide IntelliSense at design time, which makes it much easier to construct a correct query.
The results are returned from queries as strongly typed objects, so the items and fields also provide IntelliSense and compile-time checking.

I hope this is enough to understand why LINQ to SharePoint is good to use in SP2010.
Read this post for How to use LINQ

2 comments

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s