This project has moved and is read-only. For the latest updates, please go here.

How to Write Query Using CAMLEX

Oct 15, 2010 at 6:34 PM

HI First of thanks for Camlex.

how can i write following query using Camlex.

string CamlQuery = @"<Where>
                                          <FieldRef Name='ContentType' />
                                          <Value Type='Text'>Faculty Bio Page</Value>
                                <OrderBy><FieldRef Name='LastName' Ascending='True' /></OrderBy>";
                    string ViewFields = @"<FieldRef Name='FirstName' />
                                                <FieldRef Name='PublishingRollupImage' />
                                                <FieldRef Name='LastName' />
                                                <FieldRef Name='ID' />
                                                <FieldRef Name='AreaDirectory' />";

so far i did this  var CamlQuery = Camlex.Query().Where(x => (string)x["ContentType"] == "Faculty Bio Page").OrderBy(x => x["LastName"]); which cover first part.



Oct 15, 2010 at 7:07 PM

I think i got my answer there is no Select extension method so i need to set ViewFields Propert of SPQuery object.



Oct 17, 2010 at 8:24 AM

ronakce, hello

thanks for choosing Camlex. You right - currently there is no any helpers methods for ViewFields. As this is separate property of SPQuery object you can't just write something like this:

var query = new SPQuery();
query.Query = Camlex.Query().Where(x => (string)x["Title"] == "Foo")
    .ViewFields(x => x["Title"]).ToString();


as you see all changes are going to Query property. But I think we can add separate method ViewFields() into Camlex which will not be used in fluent interface method chaining (i.e. it will not return IQuery object) - instead it will return string with view fields based on parameters, i.e.:

var query = new SPQuery();
query.Query = Camlex.Query().Where(x => (string)x["Title"] == "Foo").ToString();
query.ViewFields = Camlex.Query().ViewFields(x => x["Title"]);


There will also possibility to specify several view fields like that (like we did for OrderBy() method):

query.ViewFields = Camlex.Query().ViewFields(x => new [] { x["Title"], x["Status"]});


It is not very complicated and we will implement it in near time

Oct 17, 2010 at 10:43 AM


I added new methods to IQuery interface and released version:

public interface IQuery
    string ViewFields(Expression<Func<SPListItem, object>> expr);
    string ViewFields(Expression<Func<SPListItem, object>> expr, bool includeViewFieldsTag);
    string ViewFields(Expression<Func<SPListItem, object[]>> expr);
    string ViewFields(Expression<Func<SPListItem, object[]>> expr, bool includeViewFieldsTag);


Now there is possibility to specify ViewFields using C# expressions as well as Where, OrderBy and GroupBy:

var query = new SPQuery();
query.Query = Camlex.Query().Where(x => (string)x["Title"] == "Foo").ToString();
query.ViewFields = Camlex.Query().ViewFields(x => new [] { x["Title"], x["Status"] });

as result you will have the following ViewFields property:

<FieldRef Name="Title" /><FieldRef Name="Status" />


Also you can specify whether or not parent <ViewFields></ViewFields> tags added to resulting xml by specifying true in includeViewFieldsTag parameter. Also you can use guids in indexers. In this case you will have the following ViewFields:

query.ViewFields = Camlex.Query().ViewFields(x => new [] { x[SPBuiltInFieldId.Title], x[SPBuiltInFieldId.Modified] });

it will produce the following xml:

<FieldRef ID="fa564e0f-0c70-4ab9-b863-0177e6ddd247" /><FieldRef ID="28cf69c5-fa48-462a-b5cd-27b6f9d2bd5f" />

i.e. ID attribute is used instead of Name.

So for your initial example you should write:

query.Query = Camlex.Query().ViewFields(x => new[] { x["FirstName"], x["PublishingRollupImage"], x["LastName"], x["ID"], x["AreaDirectory"] });

Oct 18, 2010 at 3:46 PM

Awesome thanks lot