Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


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

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.




Full Text Search: The Key to Better Natural Language Queries for NoSQL in Node.js

Building a CAML Query (continued)
You can nest conditional statements. For instance to retrieve items where Column2 contains an "a" or "e" and Column 1 contains GetMe! you could add the following:

query += "<Or>"; // Condtion query += "<Contains>"; // Operator query += "<FieldRef Name=\"Column2\"/>"; // Field Name query += "<Value Type=\"String\">a"; // Value query += "</Value></Contains>"; // Close tags

You'd repeat the preceding code for the letter 'e':

query += "<Contains>"; // Operator query += "<FieldRef Name=\"Column2\"/>"; // Field Name query += "<Value Type=\"String\">e"; // Value query += "</Value></Contains>"; // Close tags

Now close the nested conditional statement:

query += "</Or>";

To finish the "Filter" part of the query, you have to close any open conditional statements and the Where tag.

query += "</And></Where>";

You can see a complete example that uses the methods you've seen here to build dynamic query strings in the downloadable sample code.

You've just built a query statement that will sort Column1 in ascending order and then by Column2 in descending order, and will show only those rows where Column1 equals GetMe! and Column2 contains an "a" or "e."

You can now assign the completed string to the SPView's Query property and trigger an update so the changes will take effect.

view.Query = query; view.Update();

Edit SPViews Dyamically
The downloadable code example contains a Web Part that allows users to dynamically edit the SPView.Query property of a view called "Search." The Web Part uses the same method shown in this article to build the query string. To use the Web Part, attach it to the list's Search view page by appending ?ToolPaneView=2 to the end of the URL, for example:

Figure 2. Search Example: This sample Web Part lets you experiment with CAML by changing search parameters interactively, building CAML queries as described in the article.

http://<servername>/sites/<sitename>/Lists/ <List Name>/Search.aspx?ToolPaneView=2

You must also specify the list name in the Web Part Properties. If installed correctly, it should appear as shown in Figure 2.

Using CAML as a query language has many different applications and allows you to display your data in many different ways. It can be used with various functions provided by the SharePoint object model and can prove to be an invaluable resource if you find yourself working with lists, items, and views.

Michael Peterson currently works as a SharePoint developer for Susquehanna Technologies. He specializes in custom functionality and collaborative applications.
Comment and Contribute






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



Thanks for your registration, follow us on our social networks to keep up-to-date