Using the CAML Query Builder DLLs in your code

 
 

Using the CAML dlls in your code

  Executing a CAML query string can be done via the SPQuery class of the WSS object model. Together with the CAML Query Builder tool, we also provide you with a small assembly that encapsulates all of that code. The only thing you need to do is:
  • set a reference to U2U.SharePoint.CAML.Server.dll if you want to work via the SharePoint object model or to U2U.SharePoint.CAML.Client if you want to work via the SharePoint web services

  • declare a namespace:
    using U2U.SharePoint.CAML.Server;

  • In the load event-handler, create an object of the CAMLHelper class. You have the option in the constructor to provide it with the site, list and CAML query string in the form of an XML document or just point to the file we have saved using the Query Builder tool itself:
    CAMLHelper hlp = new CAMLHelper(Server.MapPath("~/UserControls/products.caml"));

  • You have two methods on the CAMLHelper. The ExecuteQuery returns you a DataTable, and the ExecuteXMLQuery returns you an XML string.
    DataGrid1.DataSource = hlp.ExecuteQuery();
    DataGrid1.DataBind();
 

GetListItems

 

How can you use the CAML Query Builder dlls to query lists over the SharePoint Lists web service?

Lets say I have an Employees list and I want to retrieve the first name, last name and email address of all employees who's last name starts with the letter. I want them all sorted by last name.

First you have to instantiate the U2U.SharePoint.CAML.Builder class by passing the type of query you want to perform

   U2U.SharePoint.CAML.Builder camlBuilder = new U2U.SharePoint.CAML.Builder(U2U.SharePoint.CAML.Enumerations.CamlTypes.GetListItems);                
                

Next define the fields you want to see returned in the result. This is the ViewFields part.

   camlBuilder.AddViewField("FirstName");
   camlBuilder.AddViewField("LastName");
   camlBuilder.AddViewField("EmailAddress");
                

As I want to select all employees who's last name starts with the letter D, I have to define a Where clause for the query.

   bool addCombinerNode;
   camlBuilder.AddWhereField("LastName", "D", "Text", "BeginsWith", out addCombinerNode);
                

Next step is the get the resulting list items ordered by last name:

   camlBuilder.AddOrderByField("LastName");             
                

When executing the GetListItems method of the Lists web service, you can also specify a certain number of QueryOptions. In this case you don't want to have the mandatory fields returned together with the fields you specified in the ViewFields part.

   camlBuilder.AddQueryOptionField("IncludeMandatoryColumns", false);
                

Now its time to execute the query. In this example you want to execute the GetListItems method of the Lists web service so you have to instantiate the client helper.

    U2U.SharePoint.CAML.Client.Helper helper = new U2U.SharePoint.CAML.Client.Helper(
                "http://wss.u2ucourse.com", "Employees", camlBuilder.CamlDocument);
                

Then call the GetListItems method of the client dll. The method name is called the same for your ease of use.

   XmlNode result = helper.GetListItems();   
                

In case you want to display the resulting records in a data grid, you can assign the resulting xml to a StringReader, which you can then assign to a DataSet.

   System.IO.StringReader sr = new System.IO.StringReader(result.OuterXml);
   XmlTextReader tr = new XmlTextReader(sr);
   DataSet ds = new DataSet("resultDataSet");
   ds.ReadXml(tr);
                

If no errors occurred and the resulting xml contains the selected list items, set the DataSource property of the data grid to the second table of the dataset. (The first table will contain metadata and the second table will contain the list items). To be sure you can always test on the error code in the first table before assigning the result to the data grid.

   if (ds != null && ds.Tables.Count >= 2)
       this.resultDataGrid.DataSource = ds.Tables[1];
                

 
 

UpdateListItems

 

How can you use the CAML Query Builder dlls to add a new item to a SharePoint list using the SharePoint Lists web service?

Lets say I have a U2U Contact list and I want to add a new colleague. This list contains the following columns: Last Name, First Name, Company, Business Phone and Email Address.

If you want to add a list item via the SharePoint web services, you need to execute the UpdateListItem method of the Lists web service. This method requires a CAML query that has the following syntax:

Query syntax for UpdateListItems method

So the first step is to build your CAML query. Instantiate the U2U.SharePoint.CAML.Builder class and pass it the type of method that you want to build:

   U2U.SharePoint.CAML.Builder camlBuilder = new U2U.SharePoint.CAML.Builder(U2U.SharePoint.CAML.Enumerations.CamlTypes.UpdateListItems);
                

This will create an xml document with the root node required by the update method. In this case it is <Batch>.

Then build you caml query with calls to the following methods:

   camlBuilder.UpdateBatchNode(Caml.Constants.Batch.OnError, "Continue");
   camlBuilder.AddMethodNode(1, "New");
   camlBuilder.AddFieldNode(1, "Title", "DeRudder");
   camlBuilder.AddFieldNode(1, "FirstName", "Kevin");
   camlBuilder.AddFieldNode(1, "Email", "kevin@u2u.be");
                

Now you can execute the UpdateListItems metod on the U2U.SharePoint.CAML.Client dll:

   U2U.SharePoint.CAML.Client.Helper helper = new U2U.SharePoint.CAML.Client.Helper(
        "http://wss.u2ucourse.com", "U2U Contacts", camlBuilder.CamlDocument);
                

By using this constructor of the helper class you pass SharePoint URL, list name and CAML query document in one time. So calling the following method is sufficient to get your query correctly executed:

   XmlNode result = helper.UpdateListItems();             
                

This method calls the UpdateListItem method on the Lists web service and passes the CAML to it. The returned value is an xml node that contains a success message or an error message. If the xml contains the following <ErrorCode>0x00000000</ErrorCode>, it means your query executed successfully and you item is added to the list. If the ErrorCode element contains something else, inspect the xml string closely because the error message will be contained in it.


 

An Example

 
  The CAML builder allows you to store the query as a file. Just use the File | Save File menu item. Once it is saved, we can use the server component of the CAML builder to have this query executed within our code. Just as in the previous example, we have a small ASP.NET user control that I am going to host in the smartpart. Except now I have a text box where we can enter a search string.
 
  web part
 
  Follow the steps discussed in the previous section to set the reference and then create an object of the CAML Helper. You need a reference to the following assemblies:

U2U.SharePoint.CAML.dll
U2U.SharePoint.CAML.Server.dll

Since we are having one parameter, you need to first create an array of CAMLParameter objects and add to it one CAMLParameter object. Just like before, the ExecuteQuery method returns you a ADO.NET DataTable you can immediately bind to your DataGrid.

U2U.SharePoint.CAML.Server.CAMLHelper helper = new U2U.SharePoint.CAML.Server.CAMLHelper (Server.MapPath("~/UserControls/products_v2.caml"));
U2U.SharePoint.CAML.CamlParameter[] pars = new U2U.SharePoint.CAML.CamlParameter[1];
pars[0] = new U2U.SharePoint.CAML.CamlParameter("Model",TextBox1.Text);
DataTable dt = helper.ExecuteQuery(pars);
DataGrid1.DataSource = dt;
DataGrid1.DataBind();
 
  The result can be displayed in the Web part:
 
  Result displayed in the web part
 
Contact me Contact


Contact me Receive U2U Newsletter.
Looking for a challenging job Download Brochure On Site Training Looking for a challenging job
Favorites Favorites
Copyright © 1999-2010 by U2U