CodeCaml

Necroskillz edited this page Sep 19, 2011 · 3 revisions

CodeCaml has one single entry point, that will allow you to generate CAML - class CQ.

Where queries

Standard where query

CQ.Where(CQ.Eq.FieldRef([field_name_or_id]).Value([value]))

generated CAML:

<Where><Eq><FieldRef ID|Name='[field_name_or_id]' /><Value Type='[value_type]'>[value]</Value></Eq></Where>

Value includes overloads for types string (Text), int (Integer), DateTime (DateTime) and bool (Bool). Other types can be specified through CQValueType enum or string.

Where query with or/and

CQ.Where(
    CQ.And(
        CQ.Gt.FieldRef('[field_name_or_id]').Value([value]),
        CQ.Or(
            CQ.Lt.FieldRef('[field_name_or_value]').Value([value]), 
            CQ.Neq.FieldRef('[field_name_or_id]').Value([value])
            )
        )
    )

generated CAML:

<Where>
    <And>
        <Gt>
            <FieldRef ID|Name='[field_name_or_id]' /><Value Type='[value_type]'>[value]</Value>
        </Gt>
        <Or>
            <Lt>
                <FieldRef ID|Name='[field_name_or_id]' /><Value Type='[value_type]'>[value]</Value>
            </Lt>
            </Neq>
                <FieldRef ID|Name='[field_name_or_id]' /><Value Type='[value_type]'>[value]</Value>
            </Neq>
        </Or>
    </And>
<Where>

Predefined Values

SharePoint has some CAML tags for predefined values you can make use of. For purposes of a query I implemented Now, Today, Month and UsedID. These values are accessible throught CQ.PredefinedValue.

CQ.Where(CQ.Leq.FieldRef(id).Value(CQ.PredefinedValue.Today()))

Multiple field refs (DateRangesOverlap)

For operator DateRangesOverlap, it is necessary to specify multiple field refs. This is done fluently.

CQ.Where(CQ.DateRangesOverlap.FieldRef(id1).FieldRef(id2).FieldRef(id3).Value(CQ.PredefinedValue.Month()))

Multiple values (In)

Operator In can use multiple values wrapped in Values element.

CQ.Where(CQ.In.FieldRef(id).Values(CQ.Value("val"), CQ.Value(1), CQ.PredefinedValue.UsedID()))

Setting field ref attributes (LookupId)

Sometimes attributes are needed to be set on FieldRef.

CQ.Where(CQ.Eq.FieldRef(CQ.FieldRef(id).LookupId(true)).Value(1))

Order by

CQ.OrderBy(CQ.FieldRef(id))

Group by

CQ.GroupBy(CQ.FieldRef(id))

Combining statements (Where, OrderBy, GroupBy)

CQ.Concat(
    CQ.Where(
        CQ.Eq.FieldRef(id).Value(val)
        ),
    CQ.OrderBy(
        CQ.FieldRef(id)
        )
    )

Query specification options

The api also provides methods for generating CAML for properties such as ViewFields, ViewAttributes, Webs, Lists, etc. for both SPQuery and other data queries.

Specify ViewFields

query.ViewFields = CQ.ViewFields(CQ.FieldRef(id), CQ.FieldRef(id));

Specify ViewAttributes

I could think of only Scope as an attribute used often enough to be included. You can still specify other attributes as strings.

query.ViewAttributes = CQ.ViewAttributes.Scope(CQViewScope.Recursive).Attribute("name", "value");

Specify Webs

For use in data queries.

query.Webs = CQ.Webs(CQQueryScope.SiteCollection);

Specify Lists

For use in data queries.

query.Lists = CQ.Lists(CQ.List(id), CQ.WithIndex(id, value));

Specify attributes for Lists

query.Lists = CQ.Lists(
                CQ.ListsAttributes.BaseType(CQListBaseType.DocumentLibrary)
                                  .Hidden(true)
                                  .MaxListLimit(500)
                                  .ServerTemplate(104),
                CQ.List(FieldId)
                );
Clone this wiki locally
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.