How to: View CAML Generated by LINQ to SharePoint

Overview

Microsoft® SharePoint® Server 2010 lets you use LINQ to SharePoint to take advantage of the LINQ framework and LINQ to Entities against SharePoint lists. The LINQ framework will convert LINQ to SharePoint statements into collaborative application markup language (CAML), and then execute the CAML against the SharePoint object model or Web Services.

To ensure optimization of CAML queries, it is essential that you are able to inspect the CAML generated by the LINQ framework. This how-to topic explains how you can inspect the CAML.

Ff798464.note(en-us,PandP.10).gifNote:
This how-to topic uses the SharePoint List RI as an example of a project with generated SPLinq classes. It assumes that you have already generated LINQ to SharePoint DataContext and Entity classes. For more information about how to create these references, see How to: Manually Generate LINQ to SharePoint Entity Classes by Using SPMetal.

Steps

To view the CAML generated by LINQ to SharePoint

  1. Create a new using() block that defines a new DataContext for the LINQ query, as shown in the following example.
    using (var dataContext = new YourDataContext(@"http://yourdomain/sites/yoursite"))
    {
    
    }
    
    
    Ff798464.note(en-us,PandP.10).gifNote:
    In your code, replace YourDataContext, yourdomain, and yoursite with the appropriate information for your environment.

  2. Instantiate a new StringBuilder object, as shown here.
    StringBuilder stringBuilder = new StringBuilder();
    
    
  3. Instantiate a new TextWriter object, as shown here.
    TextWriter textWriter = new StringWriter(stringBuilder);
    
    
  4. Set the .Log Property of the DataContext to the TextWriter, as shown here.
    dataContext.Log = textWriter;
    
    
  5. Write a LINQ query or LINQ Entity operation against the DataContext, as shown here.
    Ff798464.note(en-us,PandP.10).gifNote:
    The queries shown are dependent on a specific DataContext, and most likely will not function as shown if your DataContext differs.

    var departments = from department in dataContext.Departments
                where department.Title.StartsWith("deptname1")
                select department;
    
    

    or

    var departments = dataContext.Department.Where(d => d.Title.StartsWith ("deptname2"))
    
    
  6. Inspect the CAML by inspecting value of the StringBuilder.
    string queryLog = stringBuilder.ToString();
    
    

For reference, the following is an example of the complete implementation.

using (var dataContext = new PartsSiteDataContext(@http://localhost/sites/parts))
{
     StringBuilder stringBuilder = new StringBuilder();

     TextWriter textWriter = new StringWriter(stringBuilder);

     dataContext.Log = textWriter;

var departments = from department in dataContext.Departments
             where department.Title.StartsWith("deptname1")
             select department;

Assert.True(departments.Count() > -1);

string queryLog = stringBuilder.ToString();

}


Show: