Welcome to SofiaDev.Org's Blogs Sign in | Join | Help

My custom C# CAML Generator

I while ago I published a posting where I announced a series of MS Sharepoint / WSS articles to come.

Here’s the "My custom CAML generator in C#"Big Smile [:D].

So what do we have here? I wrote a simple and yet powerful generator in C# - emitting XML that can be used with SPQuery for querying SPLists.

I currently support the following:

  • Query of one/two conditions (both AND and OR)
  • Grouping (GroupBy)
  • SPQuery with field lists – you can select only the fields you need, not all of the fields in the list
  • Initialization of the SPQuery for you. You call GetQuery(string[]) or GetQuery() which will return respectively a SPQuery instance for selecting an array of fields (when you don’t need all) or all of them. This one was inspired by a guy at work asking “Do you support this?”, Well, I do now.

So, let’s see how it works.

There are a total of 5 classes that do the whole thing, and usually you need only 2 or 3 of them when you work with my generator.

The class you typically start with is called WhereClause. Once you’ve instantiate it, you need to add one or more Predicates to it. A predicate means “condition”. The predicates themselves contain terms. A term one of the conditions in a predicate. That means that if we have a T-SQL statement like:

SELECT * FROM Authors WHERE au_id=’8823832’ OR au_fname=’Branimir’

The predicate will be the whole WHERE clause and a term will be only one of the conditions inside of it. Here these are "au_id='...'" And "au_fname='...'".

In terms of our little project here, the usage looks like:

 

WhereClause where = new WhereClause();

 

Predicate p = new Predicate(ConditionType.Or);

 

p.Add(new Term("Title", "SofiaDev", FieldType.Text, EqualityType.Contains));

p.Add(new Term("Title", "April", FieldType.Text, EqualityType.Contains));

 

where.Add(p);

 

 

This is the most simple usage scenario we can have. It searches for items that contain “SofiaDev” or “April” in the title of the list. Once we’ve initialized the Predicate, we’ve started to add our simple conditions to it. Then we’ve added the predicate to the WhereClause instance.

Then we can obtain the SPQuery instance from the WhereClause instance.

Like this:

 

SPQuery query = where.GetQuery();

 

With the query instance then we can use GetItems of SPList.

Like this:

 

SPListItemCollection result = events.GetItems(query);

 

 

It’s that easy.

If on the other hand you want to select just a subset of the fields and not all of them, you can call GetQuery(string[]) where, the input array contains the field names.

Here’s how it’s done:

string[] FieldsToReturn =

{

      "Title",

      "Col1"

};

 

SPQuery queryWithFieldsSelection = where.GetQuery(FieldsToReturn);

SPListItemCollection result2 = events.GetItems(queryWithFieldsSelection);

 

You find the example along with the assembly including the CAML implementation in the attachment of this post.

Cheers,
Branimir

Published 10 Май 2006 20:34 by branimir
Filed Under: ,
Attachment(s): SofiaDev.Sharepoint.Caml.zip

Comments

No Comments

Anonymous comments are disabled