In this post, we will see how to view the CAML generated by LINQ to SharePoint. As we know when we write a LINQ query against a SharePoint List, internally the query gets converted into ‘CAML’ (Collaborative Application Markup Language’). I have already written an article on ‘LINQ to SharePoint’ in which I have explained how to perform CRUD operation on SharePoint Lists. If you have not seen that article, I would highly recommend you to go through the article first and then you can continue with this one.
So let’s create a simple console application to implement this demonstration. Once you create a console application, you will have to follow a couple of steps. These steps are listed in my article ‘LINQ to SharePoint’
The steps are –
1) First you need to generate Entities for all the lists in SharePoint Site with their properties using the SPMetal.exe tool.
2) Once you generate the .CS/.VB file with all the entities, use this file for querying the SharePoint Lists data. So let’s add this file into our Console application.
3) Now with the generated file in our console application, we now have to add the reference to an API – Microsoft.SharePoint.LINQ.dll. You will find ths DLL file in the following path – C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI.
Now in the ‘Main’ method, let’s write the following code as shown below –
If you observe the above code, we are declaring an object of the data context of our SharePoint site by passing a URL of our site. We then write a LINQ query which fetches the data from ‘Customers’ list using a where condition. We are using the ‘Log’ property of our data context which shows the LINQ query conversion to CAML.
Note: Please replace the site URL in the code above, with your site URL.
Download the source code