Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Using SharePoint's SPView Class and CAML as a Query Language : Page 2

Learn how to use Microsoft's XML-based Collaborative Application Markup Language (CAML) as a query language, letting you find and display SharePoint items dynamically.


advertisement
Building a CAML Query
Now let's get into the actual building of the query string. The first but optional part of the string is the "Sort." Here you can specify which columns you would like the data sorted by and specify an ascending or descending sort order.

If you are going to include any type of sorting the query string needs to start with an <OrderBy> tag (that you must eventually close with </OrderBy>).

String query = "<OrderBy>";

Next, specify the field name (Column) that you would like to sort by.

query += "<FieldRef Name=\"Cloumn1\">";

By default SharePoint performs the sort in ascending order. To specify a descending order, add the following attribute:



query += "<FieldRef Name=\"Cloumn2\" Ascending=\"FALSE\"/>";

Author's Note: Spaces are not allowed in column names! You must replace spaces with their hex representation: _x0020_. For example, you'd enter a column named "Column 1" as Column_x0020_1.

You can include as many column entries as needed, using the same syntax shown above. When you're done, remember to close the "Sort" part of the query.

query += "</OrderBy>";

Now comes the "Filter" part, which can get a little tricky. That part begins with a tag that is much like the WHERE clause in a SQL SELECT statement.

query += "<Where>";

If you have multiple conditions, you can choose from a group of conditional statements that CAML provides, including the basic: <And> and <Or>.

query += "<And>";

Of course, you can omit this part for single-condition statements, using only a single statement inside your tag. For this article I'll use <And> to retrieve items that satisfy all the specified criteria.

Next, you specify operators, which include such possibilities as <Contains>, and <BeginsWith> for strings, <Gt> (greater than), and <Lt> (less than) for numeric data, and <Eq> (equals) for both.

query += "<Eq>";

Next specify the field name much like you did for the "Sort" part of the query. Remember that you can't include spaces in field names!

query += "<FieldRef Name=\"Column1\"/>";

After that, specify the value type and the value that the item must satisfy. The following example searches for items where the value in the Column1 field equals GetMe!.

query += "<Value Type=\"String\">GetMe!";

After completing the conditions, remember to close the Value and operator tags.

query += "</Value></Eq>";



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap