PostRank Topblogs 2009 - #20 in Sharepoint

Windows Live Alerts web tracker
Chat with me if I'm online!
search blog
most popular
MCP MCTS MCT MVP

SP 2010: LINQ to SharePoint - What CAML lies behind my query? 

Author: Tobias Zimmergren
http://www.zimmergren.net | http://www.tozit.com | @zimmergren

Introduction

The two following questions are quite popular in my SharePoint 2010 developer training classes, so I ought to answer them right here.

  1. "What does LINQ to SharePoint really do?"
  2. "Can I see the CAML query generated by the LINQ to SharePoint query?"

The answer is simple: Yes, you can see the results of your generated LINQ query by using the Log property of your DataContext object.

What CAML lied behind my LINQ to SharePoint query?

In order to fetch the CAML query that lies behind your LINQ query, all you need is to work with the .Log object of your DataContext.

See this simple example, which simply outputs the CAML query to an XML-file for easy reading:

image

This will essentially generate the following content in the file C:\MyEntitiesDataContextQuery.xml:

image

As you can see, the LINQ to SharePoint query is automatically turned into a CAML Query.

Summary

Yep, all you need is the .Log property to fetch the CAML query from your LINQ statement. In my sample I'm outputting it to a file called C:\MyEntitiesDataContextQuery.xml.

You could of course output it in any other way you want - not just a physical file on the file system. The Log property is of the type TextWriter.

Enjoy!

 
Posted on 19-Feb-10 by Tobias Zimmergren
8 Comments  |  Trackback Url  |  Link to this post | Bookmark this post with:        
Tags: 2010, CAML, LINQ, LINQ to SharePoint, SP2010
 

Links to this post (Trackbacks/Pingbacks)

Albuterol cr.

Trackback from Albuterol cr. on 31 Jul 2010 11:09

Comments

Saturday, 20 Feb 2010 06:37 by Mohan
What are some scenarios where CAML will need to be used directly instead of running LINQ to SharePoint?

Saturday, 20 Feb 2010 11:29 by Mohan
What are some scenarios where CAML will need to be used directly instead of running LINQ to SharePoint?

Sunday, 21 Feb 2010 09:56 by Adam
gr8 tip!!

Sunday, 28 Feb 2010 02:56 by ItsMeSri
Where can I get start point learning LINQ to Sharepoint? Provide us some links.

Sunday, 28 Feb 2010 01:45 by Tobias Zimmergren
ItsMeSri: Take a look here http://www.zimmergren.net/archive/2010/02/19/sp-2010-getting-started-with-linq-to-sharepoint-in-sharepoint-2010.aspx

Tuesday, 2 Mar 2010 11:47 by Abdel-Rahman Awad
Hi Tobias was trying linq to sharepoint list with attached files .. but the thing is linq doesnt show the attachments properties any luck / knowledge about that ?

Tuesday, 1 Jun 2010 05:03 by

Tuesday, 29 Jun 2010 05:44 by

Name:
URL:
Email:
Comments: