f you've been working to customize SharePoint, you've probably at least heard the term "CAML," which stands for Collaborative Application Markup Language. CAML is the XML-based language that Microsoft uses to build and customize SharePoint sites. Many of SharePoint's .site
definition XML files are written in CAML. However, CAML is capable of more than site definitions; another CAML featureand one that seems to be far less documentedis that you can use it as a query language. CAML queries allow you to dynamically find and display SharePoint items based on various criteria. If you've ever created a List View you may not have been aware of what was really going on.
|Figure 1. Creating a List View: When you create a List View, SharePoint automatically creates a CAML query.|
When you specify a "Sort" or "Filter" during the List View creation process, you are creating the value for the SPView object's Query
value (see Figure 1
). The SPView class is the programmatic representation of a List View. You can view the generated List View query programmatically by simply exposing the string return value from the SPView.Query
property, as you'll see below. But you aren't limited to creating CAML queries through dialog interaction. This article discusses how you can create such CAML queries programmatically.
CAML Query Statements
There are two main parts to a CAML query statement: The first is straightforward but the second can become a little more complicated. From now on I will refer to them as the "Sort" and "Filter" parts.
For the following example create a List View called "Search," leaving all the default values. The newly created SPView object is accessible from the SPWeb object.
SPWeb web = SPControl.GetContextWeb(this.Context);
SPView view = web.Lists["List Name"].Views["Search"];
You can get or set the view's query string through its Query
string query = view.Query;
view.Query = query;
|Author's Note: You must call update for the changes to take effect.