Quantcast
Channel: CamlJs: SharePoint JavaScript Caml Builder
Viewing all 199 articles
Browse latest View live

Updated Wiki: Home

$
0
0

Project Description

Simplifies creation of SharePoint CAML queries for client-side scripts. Can be used with either SharePoint Client Object Model or SPServices.

Nuget:

PM> Install-Package CamlJs

TypeScript definitions:

PM> Install-Package camljs.TypeScript.DefinitelyTyped

What's new in Release 2.5

View element

View element is added! Scope attribute, ViewFields, Joins and ProjectedFields are supported. Joining lists via CamlJs is very easy. Here's the example:

var query = new CamlBuilder()
    .View(["Title","Country","Population"])
    .LeftJoin("Country","Country").Select("People","Population")
    .Query()
    .Where()
    .NumberField("Population").LessThan(10)
    .ToString();

Intellisense will help to understand the parameters:

And one more screenshot of intellisense:

This code generates the following CAML query:

<View><ViewFields><FieldRefName="Title"/><FieldRefName="Country"/><FieldRefName="Population"/></ViewFields><Joins><JoinType="LEFT"ListAlias="Country"><Eq><FieldRefName="Country"RefType="ID"/><FieldRefName="ID"List="Country"/></Eq></Join></Joins><ProjectedFields><FieldShowField="People"Type="Lookup"Name="Population"List="Country"/></ProjectedFields><Query><Where><Lt><FieldRefName="Population"/><ValueType="Number">10</Value></Lt></Where></Query></View>

No dependencies

CamlJs was dependent on some SharePoint components (Sys.StringBuilder and SP.XmlWriter). But many people had problems with that, so now CamlJs doesn't have any dependencies.

Migration from 2.0 to 2.5

Please remember that some fields were marked as DEPRECATED in Release 2.0. In version 2.5, previously marked LookupIdField is removed. In next versions, the process will be continued.

If you're still using LookupIdField, replace it like this:

Old code example:

.LookupIdField("MyLookup").EqualTo(5)

Replace with:

.LookupField("MyLookup").Id().EqualTo(5)

ToCamlQuery()

Added ToCamlQuery method, that can be to finalize the query. But instead of string, ToCamlQuery will produce a ready-to-use SP.CamlQuery object, which you can then pass into list.getItems method.

Example project

Example project is now a SharePoint-hosted app with a simple interactive demo:

UserMultiField and LookupMultiField

UserMultiField and LookupMultiField weren't much tested in previous release, so in 2.5 they are tested through and significantly improved. Now instead old EqualTo, NotEqualTo, Includes and NotIncludes operations (these are marked DEPRECATED), single operation"IncludesSuchItemThat" added.

 

So now it is possible to check condition against Id or against a value, cast to a specified type (same as with single-value lookups):

Internally, CAML Query is identical for MultiLookup and Lookup columns, but hopefully this minor syntax improvement will help people better understand how SharePoint interprets this query.

---------------- end of release 2.5 news -------------

Basics

To start with, let's assume we need to fetch all Google-related letters from some mailbox list, using SharePoint Client Object Model. To generate the corresponding query using Caml Builder, you could use following javascript code:

var camlBuilder = new CamlBuilder();

    var caml = camlBuilder.Where()
        .TextField("Email").EqualTo("support@google.com")
        .Or()
        .TextField("Email").EqualTo("plus@google.com")
        .Or()
        .TextField("Title").BeginsWith("[Google]")
        .Or()
        .TextField("Content").Contains("Google")
        .ToString();

This will generate expected CAML:

<Where><Or><Eq><FieldRefName="Email"/><ValueType="Text">support@google.com</Value></Eq><Or><Eq><FieldRefName="Email"/><ValueType="Text">plus@google.com</Value></Eq><Or><BeginsWith><FieldRefName="Title"/><ValueType="Text">[Google]</Value></BeginsWith><Contains><FieldRefName="Content"/><ValueType="Text">Google</Value></Contains></Or></Or></Or></Where>

, which then could be used in SP.CamlQuery.

Very good so far, and have a look at some other examples!

First of all, cannot resist to mention, that CamlBuilder covers almost all Query elements, described at MSDN.

For example, seldom used Membership element:

var caml = camlBuilder.Where()
        .UserField("AssignedTo").EqualToCurrentUser()
        .Or()
        .UserField("AssignedTo").IsInCurrentUserGroups()
        .GroupBy("ProductTitle")
        .OrderBy("Priority").ThenBy("Title")
        .ToString();

This code will generate following CAML:

<Where><Or><Eq><FieldRefName="AssignedTo"/><ValueType="Integer"><UserID/></Value></Eq><MembershipType="CurrentUserGroups"><FieldRefName="AssignedTo"/></Membership></Or></Where><GroupBy><FieldRefName="ProductTitle"/></GroupBy><OrderBy><FieldRefName="Priority"/><FieldRefName="Title"/></OrderBy>

The last example at this section:

    caml = camlBuilder.Where()
        .LookupField("Category").Id().In([2, 3, 10])
        .And()
        .DateField("ExpirationDate").LessThanOrEqualTo(CamlBuilder.CamlValues.Now)
        .OrderByDesc("ExpirationDate")
        .ToString()

As you see, the code is pretty clean and readable. The resulting CAML is much more awkward, especially if you imagine it in javascript strings dress, without indentation and highlighting...

<Where><And><In><FieldRefName="Category"LookupId="True"/><Values><ValueType="Integer">2</Value><ValueType="Integer">3</Value><ValueType="Integer">10</Value></Values></In><Leq><FieldRefName="ExpirationDate"/><ValueType="Date"><Now/></Value></Leq></And></Where><OrderBy><FieldRefName="ExpirationDate"Ascending="False"/></OrderBy>

Usability

Besides of readability, CamlBuilder pretends to be very handy in development, because you can use intellisense and inline documentation:

This will help to catch a bunch of errors when writing code, and surely prevent from typos and misunderstandings, so the benefits could be really magnificient!

Contribute

Any contributions are highly appreciated!


Updated Wiki: Home

$
0
0

Project Description

Simplifies creation of SharePoint CAML queries for client-side scripts. Can be used with either SharePoint Client Object Model or SPServices.

Nuget:

PM> Install-Package CamlJs

TypeScript definitions:

PM> Install-Package camljs.TypeScript.DefinitelyTyped

What's new in Release 2.5

View element

View element is added! Scope attribute, ViewFields, Joins and ProjectedFields are supported. Joining lists via CamlJs is very easy. Here's the example:

var query = new CamlBuilder()
    .View(["Title","Country","Population"])
    .LeftJoin("Country","Country").Select("People","Population")
    .Query()
    .Where()
    .NumberField("Population").LessThan(10)
    .ToString();

Intellisense helps understanding the parameters:

And one more screenshot of intellisense:

So as you see, really simple: Join listName as Alias, Select Field as Alias, Select Field as Alias, etc. Then, use fields aliases in the main query. That's it! Corresponding Joins and ProjectedFields elements are generated automatically.

So the resulting generated CAML query will be the following:

<View><ViewFields><FieldRefName="Title"/><FieldRefName="Country"/><FieldRefName="Population"/></ViewFields><Joins><JoinType="LEFT"ListAlias="Country"><Eq><FieldRefName="Country"RefType="ID"/><FieldRefName="ID"List="Country"/></Eq></Join></Joins><ProjectedFields><FieldShowField="People"Type="Lookup"Name="Population"List="Country"/></ProjectedFields><Query><Where><Lt><FieldRefName="Population"/><ValueType="Number">10</Value></Lt></Where></Query></View>

No dependencies

CamlJs was dependent on some SharePoint components (Sys.StringBuilder and SP.XmlWriter). But many people had problems with that, so now CamlJs doesn't have any dependencies.

This enables opportunity to run CamlJs standalone, e.g. in SharePoint Apps without including heavy SP javascripts; and also to use CamlJs in SharePoint 2007. Though please remember that some CAML Query elements (e.g. <In>) were added only in SP2010.

Migration from 2.0 to 2.5

Please remember that some fields were marked as DEPRECATED in Release 2.0. In version 2.5, previously marked LookupIdField is removed. In next versions, the process will be continued.

If you're still using LookupIdField, replace it like this:

Old code example:

.LookupIdField("MyLookup").EqualTo(5)

Replace with:

.LookupField("MyLookup").Id().EqualTo(5)

ToCamlQuery()

Added ToCamlQuery method, that can be to finalize the query. But instead of string, ToCamlQuery will produce a ready-to-use SP.CamlQuery object, which you can then pass into list.getItems method.

Example project

Example project is now a SharePoint-hosted app with a simple interactive demo:

UserMultiField and LookupMultiField

UserMultiField and LookupMultiField weren't much tested in previous release, but now in 2.5 they are tested through and significantly improved. Now instead old EqualTo, NotEqualTo, Includes and NotIncludes operations (these are marked DEPRECATED), single operation "IncludesSuchItemThat" added.

 

So now it is possible to check condition against Id or against a value, cast to a specified type (same as with single-value lookups):

Internally, CAML Query is identical for MultiLookup and single-value Lookup columns, but hopefully this minor syntax improvement will help people better understand how SharePoint interprets this query.

---------------- end of release 2.5 news -------------

Basics

To start with, let's assume we need to fetch all Google-related letters from some mailbox list, using SharePoint Client Object Model. To generate the corresponding query using Caml Builder, you could use following javascript code:

var camlBuilder = new CamlBuilder();var caml = camlBuilder.Where()
        .TextField("Email").EqualTo("support@google.com")
        .Or()
        .TextField("Email").EqualTo("plus@google.com")
        .Or()
        .TextField("Title").BeginsWith("[Google]")
        .Or()
        .TextField("Content").Contains("Google")
        .ToString();

This will generate expected CAML:

<Where><Or><Eq><FieldRefName="Email"/><ValueType="Text">support@google.com</Value></Eq><Or><Eq><FieldRefName="Email"/><ValueType="Text">plus@google.com</Value></Eq><Or><BeginsWith><FieldRefName="Title"/><ValueType="Text">[Google]</Value></BeginsWith><Contains><FieldRefName="Content"/><ValueType="Text">Google</Value></Contains></Or></Or></Or></Where>

, which then could be used in SP.CamlQuery.

Very good so far, and have a look at some other examples!

First of all, cannot resist to mention, that CamlBuilder covers almost all Query elements, described at MSDN.

For example, seldom used Membership element:

var caml = camlBuilder.Where()
        .UserField("AssignedTo").EqualToCurrentUser()
        .Or()
        .UserField("AssignedTo").IsInCurrentUserGroups()
        .GroupBy("ProductTitle")
        .OrderBy("Priority").ThenBy("Title")
        .ToString();

This code will generate following CAML:

<Where><Or><Eq><FieldRefName="AssignedTo"/><ValueType="Integer"><UserID/></Value></Eq><MembershipType="CurrentUserGroups"><FieldRefName="AssignedTo"/></Membership></Or></Where><GroupBy><FieldRefName="ProductTitle"/></GroupBy><OrderBy><FieldRefName="Priority"/><FieldRefName="Title"/></OrderBy>

The last example at this section:

    caml = camlBuilder.Where()
        .LookupField("Category").Id().In([2, 3, 10])
        .And()
        .DateField("ExpirationDate").LessThanOrEqualTo(CamlBuilder.CamlValues.Now)
        .OrderByDesc("ExpirationDate")
        .ToString()

As you see, the code is pretty clean and readable. The resulting CAML is much more awkward, especially if you imagine it in javascript strings dress, without indentation and highlighting...

<Where><And><In><FieldRefName="Category"LookupId="True"/><Values><ValueType="Integer">2</Value><ValueType="Integer">3</Value><ValueType="Integer">10</Value></Values></In><Leq><FieldRefName="ExpirationDate"/><ValueType="Date"><Now/></Value></Leq></And></Where><OrderBy><FieldRefName="ExpirationDate"Ascending="False"/></OrderBy>

Usability

CamlBuilder is not only readable and simple, but it also provides rich intellisense and inline documentation:

This will help to catch a bunch of errors when writing code, and surely prevent from typos and misunderstandings, so the benefits could be really magnificient!

Intellisense and inline docs are even better if you use CamlJs with TypeScript.

Contribute

Any contributions are highly appreciated!

Source code checked in, #d1b1ac680ea28f25a4b07bbf76401b01dbb7a54e

$
0
0
fix for the View Scope bug, reported by @MannD

Source code checked in, #fab8f2628bc420532440e3252c59ab1e36e6f12f

Updated Wiki: Home

$
0
0

Project Description

Simplifies creation of SharePoint CAML queries for client-side scripts. Can be used with either SharePoint Client Object Model or SPServices.

Nuget:

PM> Install-Package CamlJs

TypeScript definitions:

PM> Install-Package camljs.TypeScript.DefinitelyTyped

Check out CamlJs-Console!

I created a Chrome extension for instant creating and testing CamlJs queries against real lists,CamlJs-Console. The extension is open source and free. Available via Web Store or alternatively you can install it from the source code (see docs on GitHub).

What's new in Release 2.5

View element

View element is added! Scope attribute, ViewFields, Joins and ProjectedFields are supported. Joining lists via CamlJs is very easy. Here's the example:

var query = new CamlBuilder()
    .View(["Title","Country","Population"])
    .LeftJoin("Country","Country").Select("People","Population")
    .Query()
    .Where()
    .NumberField("Population").LessThan(10)
    .ToString();

Intellisense helps understanding the parameters:

And one more screenshot of intellisense:

So as you see, really simple: Join listName as Alias, Select Field as Alias, Select Field as Alias, etc. Then, use fields aliases in the main query. That's it! Corresponding Joins and ProjectedFields elements are generated automatically.

So the resulting generated CAML query will be the following:

<View><ViewFields><FieldRefName="Title"/><FieldRefName="Country"/><FieldRefName="Population"/></ViewFields><Joins><JoinType="LEFT"ListAlias="Country"><Eq><FieldRefName="Country"RefType="ID"/><FieldRefName="ID"List="Country"/></Eq></Join></Joins><ProjectedFields><FieldShowField="People"Type="Lookup"Name="Population"List="Country"/></ProjectedFields><Query><Where><Lt><FieldRefName="Population"/><ValueType="Number">10</Value></Lt></Where></Query></View>

No dependencies

CamlJs was dependent on some SharePoint components (Sys.StringBuilder and SP.XmlWriter). But many people had problems with that, so now CamlJs doesn't have any dependencies.

This enables opportunity to run CamlJs standalone, e.g. in SharePoint Apps without including heavy SP javascripts; and also to use CamlJs in SharePoint 2007. Though please remember that some CAML Query elements (e.g. <In>) were added only in SP2010.

Migration from 2.0 to 2.5

Please remember that some fields were marked as DEPRECATED in Release 2.0. In version 2.5, previously marked LookupIdField is removed. In next versions, the process will be continued.

If you're still using LookupIdField, replace it like this:

Old code example:

.LookupIdField("MyLookup").EqualTo(5)

Replace with:

.LookupField("MyLookup").Id().EqualTo(5)

ToCamlQuery()

Added ToCamlQuery method, that can be to finalize the query. But instead of string, ToCamlQuery will produce a ready-to-use SP.CamlQuery object, which you can then pass into list.getItems method.

Example project

Example project is now a SharePoint-hosted app with a simple interactive demo:

UserMultiField and LookupMultiField

UserMultiField and LookupMultiField weren't much tested in previous release, but now in 2.5 they are tested through and significantly improved. Now instead old EqualTo, NotEqualTo, Includes and NotIncludes operations (these are marked DEPRECATED), single operation "IncludesSuchItemThat" added.

 

So now it is possible to check condition against Id or against a value, cast to a specified type (same as with single-value lookups):

Internally, CAML Query is identical for MultiLookup and single-value Lookup columns, but hopefully this minor syntax improvement will help people better understand how SharePoint interprets this query.

---------------- end of release 2.5 news -------------

Basics

To start with, let's assume we need to fetch all Google-related letters from some mailbox list, using SharePoint Client Object Model. To generate the corresponding query using Caml Builder, you could use following javascript code:

var camlBuilder = new CamlBuilder();

    var caml = camlBuilder.Where()
        .TextField("Email").EqualTo("support@google.com")
        .Or()
        .TextField("Email").EqualTo("plus@google.com")
        .Or()
        .TextField("Title").BeginsWith("[Google]")
        .Or()
        .TextField("Content").Contains("Google")
        .ToString();

This will generate expected CAML:

<Where><Or><Eq><FieldRefName="Email"/><ValueType="Text">support@google.com</Value></Eq><Or><Eq><FieldRefName="Email"/><ValueType="Text">plus@google.com</Value></Eq><Or><BeginsWith><FieldRefName="Title"/><ValueType="Text">[Google]</Value></BeginsWith><Contains><FieldRefName="Content"/><ValueType="Text">Google</Value></Contains></Or></Or></Or></Where>

, which then could be used in SP.CamlQuery.

Very good so far, and have a look at some other examples!

First of all, cannot resist to mention, that CamlBuilder covers almost all Query elements, described at MSDN.

For example, seldom used Membership element:

var caml = camlBuilder.Where()
        .UserField("AssignedTo").EqualToCurrentUser()
        .Or()
        .UserField("AssignedTo").IsInCurrentUserGroups()
        .GroupBy("ProductTitle")
        .OrderBy("Priority").ThenBy("Title")
        .ToString();

This code will generate following CAML:

<Where><Or><Eq><FieldRefName="AssignedTo"/><ValueType="Integer"><UserID/></Value></Eq><MembershipType="CurrentUserGroups"><FieldRefName="AssignedTo"/></Membership></Or></Where><GroupBy><FieldRefName="ProductTitle"/></GroupBy><OrderBy><FieldRefName="Priority"/><FieldRefName="Title"/></OrderBy>

The last example at this section:

    caml = camlBuilder.Where()
        .LookupField("Category").Id().In([2, 3, 10])
        .And()
        .DateField("ExpirationDate").LessThanOrEqualTo(CamlBuilder.CamlValues.Now)
        .OrderByDesc("ExpirationDate")
        .ToString()

As you see, the code is pretty clean and readable. The resulting CAML is much more awkward, especially if you imagine it in javascript strings dress, without indentation and highlighting...

<Where><And><In><FieldRefName="Category"LookupId="True"/><Values><ValueType="Integer">2</Value><ValueType="Integer">3</Value><ValueType="Integer">10</Value></Values></In><Leq><FieldRefName="ExpirationDate"/><ValueType="Date"><Now/></Value></Leq></And></Where><OrderBy><FieldRefName="ExpirationDate"Ascending="False"/></OrderBy>

Usability

CamlBuilder is not only readable and simple, but it also provides rich intellisense and inline documentation:

This will help to catch a bunch of errors when writing code, and surely prevent from typos and misunderstandings, so the benefits could be really magnificient!

Intellisense and inline docs are even better if you use CamlJs with TypeScript.

Contribute

Any contributions are highly appreciated!

Updated Wiki: Home

$
0
0

Project Description

Simplifies creation of SharePoint CAML queries for client-side scripts. Can be used with either SharePoint Client Object Model or SPServices.

Nuget:

PM> Install-Package CamlJs

TypeScript definitions:

PM> Install-Package camljs.TypeScript.DefinitelyTyped

Check out CamlJs-Console!

I created a Chrome extension for instant creating and testing CamlJs queries against real lists,CamlJs-Console. The extension is open source and free. Available via Web Store or alternatively you can install it from the source code (see docs on GitHub).

What's new in Release 2.5

View element

View element is added! Scope attribute, ViewFields, Joins and ProjectedFields are supported. Joining lists via CamlJs is very easy. Here's the example:

var query = new CamlBuilder()
    .View(["Title","Country","Population"])
    .LeftJoin("Country","Country").Select("People","Population")
    .Query()
    .Where()
    .NumberField("Population").LessThan(10)
    .ToString();

Intellisense helps understanding the parameters:

And one more screenshot of intellisense:

So as you see, really simple: Join listName as Alias, Select Field as Alias, Select Field as Alias, etc. Then, use fields aliases in the main query. That's it! Corresponding Joins and ProjectedFields elements are generated automatically.

So the resulting generated CAML query will be the following:

<View><ViewFields><FieldRefName="Title"/><FieldRefName="Country"/><FieldRefName="Population"/></ViewFields><Joins><JoinType="LEFT"ListAlias="Country"><Eq><FieldRefName="Country"RefType="ID"/><FieldRefName="ID"List="Country"/></Eq></Join></Joins><ProjectedFields><FieldShowField="People"Type="Lookup"Name="Population"List="Country"/></ProjectedFields><Query><Where><Lt><FieldRefName="Population"/><ValueType="Number">10</Value></Lt></Where></Query></View>

No dependencies

CamlJs was dependent on some SharePoint components (Sys.StringBuilder and SP.XmlWriter). But many people had problems with that, so now CamlJs doesn't have any dependencies.

This enables opportunity to run CamlJs standalone, e.g. in SharePoint Apps without including heavy SP javascripts; and also to use CamlJs in SharePoint 2007. Though please remember that some CAML Query elements (e.g. <In>) were added only in SP2010.

Migration from 2.0 to 2.5

Please remember that some fields were marked as DEPRECATED in Release 2.0. In version 2.5, previously marked LookupIdField is removed. In next versions, the process will be continued.

If you're still using LookupIdField, replace it like this:

Old code example:

.LookupIdField("MyLookup").EqualTo(5)

Replace with:

.LookupField("MyLookup").Id().EqualTo(5)

ToCamlQuery()

Added ToCamlQuery method, that can be to finalize the query. But instead of string, ToCamlQuery will produce a ready-to-use SP.CamlQuery object, which you can then pass into list.getItems method.

Example project

Example project is now a SharePoint-hosted app with a simple interactive demo:

UserMultiField and LookupMultiField

UserMultiField and LookupMultiField weren't much tested in previous release, but now in 2.5 they are tested through and significantly improved. Now instead old EqualTo, NotEqualTo, Includes and NotIncludes operations (these are marked DEPRECATED), single operation "IncludesSuchItemThat" added.

 

So now it is possible to check condition against Id or against a value, cast to a specified type (same as with single-value lookups):

Internally, CAML Query is identical for MultiLookup and single-value Lookup columns, but hopefully this minor syntax improvement will help people better understand how SharePoint interprets this query.

---------------- end of release 2.5 news -------------

Basics

To start with, let's assume we need to fetch all Google-related letters from some mailbox list, using SharePoint Client Object Model. To generate the corresponding query using Caml Builder, you could use following javascript code:

var camlBuilder = new CamlBuilder();

    var caml = camlBuilder.Where()
        .TextField("Email").EqualTo("support@google.com")
        .Or()
        .TextField("Email").EqualTo("plus@google.com")
        .Or()
        .TextField("Title").BeginsWith("[Google]")
        .Or()
        .TextField("Content").Contains("Google")
        .ToString();

This will generate expected CAML:

<Where><Or><Eq><FieldRefName="Email"/><ValueType="Text">support@google.com</Value></Eq><Or><Eq><FieldRefName="Email"/><ValueType="Text">plus@google.com</Value></Eq><Or><BeginsWith><FieldRefName="Title"/><ValueType="Text">[Google]</Value></BeginsWith><Contains><FieldRefName="Content"/><ValueType="Text">Google</Value></Contains></Or></Or></Or></Where>

, which then could be used in SP.CamlQuery.

Very good so far, and have a look at some other examples!

First of all, cannot resist to mention, that CamlBuilder covers almost all Query elements, described at MSDN.

For example, seldom used Membership element:

var caml = camlBuilder.Where()
        .UserField("AssignedTo").EqualToCurrentUser()
        .Or()
        .UserField("AssignedTo").IsInCurrentUserGroups()
        .GroupBy("ProductTitle")
        .OrderBy("Priority").ThenBy("Title")
        .ToString();

This code will generate following CAML:

<Where><Or><Eq><FieldRefName="AssignedTo"/><ValueType="Integer"><UserID/></Value></Eq><MembershipType="CurrentUserGroups"><FieldRefName="AssignedTo"/></Membership></Or></Where><GroupBy><FieldRefName="ProductTitle"/></GroupBy><OrderBy><FieldRefName="Priority"/><FieldRefName="Title"/></OrderBy>

The last example at this section:

    caml = camlBuilder.Where()
        .LookupField("Category").Id().In([2, 3, 10])
        .And()
        .DateField("ExpirationDate").LessThanOrEqualTo(CamlBuilder.CamlValues.Now)
        .OrderByDesc("ExpirationDate")
        .ToString()

As you see, the code is pretty clean and readable. The resulting CAML is much more awkward, especially if you imagine it in javascript strings dress, without indentation and highlighting...

<Where><And><In><FieldRefName="Category"LookupId="True"/><Values><ValueType="Integer">2</Value><ValueType="Integer">3</Value><ValueType="Integer">10</Value></Values></In><Leq><FieldRefName="ExpirationDate"/><ValueType="Date"><Now/></Value></Leq></And></Where><OrderBy><FieldRefName="ExpirationDate"Ascending="False"/></OrderBy>

Usability

CamlBuilder is not only readable and simple, but it also provides rich intellisense and inline documentation:

This will help to catch a bunch of errors when writing code, and surely prevent from typos and misunderstandings, so the benefits could be really magnificient!

Intellisense and inline docs are even better if you use CamlJs with TypeScript.

Contribute

Any contributions are highly appreciated!

Updated Wiki: Home

$
0
0

Project Description

Simplifies creation of SharePoint CAML queries for client-side scripts. Can be used with either SharePoint Client Object Model or SPServices.

Nuget:

PM> Install-Package CamlJs

TypeScript definitions:

PM> Install-Package camljs.TypeScript.DefinitelyTyped

Check out CamlJs-Console!

I created a Chrome extension for instant testing CamlJs queries against real lists,CamlJs-Console. The extension is open source and free. Available via Web Store or alternatively you can install it from the source code (see docs on GitHub).

What's new in Release 2.5

View element

View element is added! Scope attribute, ViewFields, Joins and ProjectedFields are supported. Joining lists via CamlJs is very easy. Here's the example:

var query = new CamlBuilder()
    .View(["Title","Country","Population"])
    .LeftJoin("Country","Country").Select("People","Population")
    .Query()
    .Where()
    .NumberField("Population").LessThan(10)
    .ToString();

Intellisense helps understanding the parameters:

And one more screenshot of intellisense:

So as you see, really simple: Join listName as Alias, Select Field as Alias, Select Field as Alias, etc. Then, use fields aliases in the main query. That's it! Corresponding Joins and ProjectedFields elements are generated automatically.

So the resulting generated CAML query will be the following:

<View><ViewFields><FieldRefName="Title"/><FieldRefName="Country"/><FieldRefName="Population"/></ViewFields><Joins><JoinType="LEFT"ListAlias="Country"><Eq><FieldRefName="Country"RefType="ID"/><FieldRefName="ID"List="Country"/></Eq></Join></Joins><ProjectedFields><FieldShowField="People"Type="Lookup"Name="Population"List="Country"/></ProjectedFields><Query><Where><Lt><FieldRefName="Population"/><ValueType="Number">10</Value></Lt></Where></Query></View>

No dependencies

CamlJs was dependent on some SharePoint components (Sys.StringBuilder and SP.XmlWriter). But many people had problems with that, so now CamlJs doesn't have any dependencies.

This enables opportunity to run CamlJs standalone, e.g. in SharePoint Apps without including heavy SP javascripts; and also to use CamlJs in SharePoint 2007. Though please remember that some CAML Query elements (e.g. <In>) were added only in SP2010.

Migration from 2.0 to 2.5

Please remember that some fields were marked as DEPRECATED in Release 2.0. In version 2.5, previously marked LookupIdField is removed. In next versions, the process will be continued.

If you're still using LookupIdField, replace it like this:

Old code example:

.LookupIdField("MyLookup").EqualTo(5)

Replace with:

.LookupField("MyLookup").Id().EqualTo(5)

ToCamlQuery()

Added ToCamlQuery method, that can be to finalize the query. But instead of string, ToCamlQuery will produce a ready-to-use SP.CamlQuery object, which you can then pass into list.getItems method.

Example project

Example project is now a SharePoint-hosted app with a simple interactive demo:

UserMultiField and LookupMultiField

UserMultiField and LookupMultiField weren't much tested in previous release, but now in 2.5 they are tested through and significantly improved. Now instead old EqualTo, NotEqualTo, Includes and NotIncludes operations (these are marked DEPRECATED), single operation "IncludesSuchItemThat" added.

 

So now it is possible to check condition against Id or against a value, cast to a specified type (same as with single-value lookups):

Internally, CAML Query is identical for MultiLookup and single-value Lookup columns, but hopefully this minor syntax improvement will help people better understand how SharePoint interprets this query.

---------------- end of release 2.5 news -------------

Basics

To start with, let's assume we need to fetch all Google-related letters from some mailbox list, using SharePoint Client Object Model. To generate the corresponding query using Caml Builder, you could use following javascript code:

var camlBuilder = new CamlBuilder();

    var caml = camlBuilder.Where()
        .TextField("Email").EqualTo("support@google.com")
        .Or()
        .TextField("Email").EqualTo("plus@google.com")
        .Or()
        .TextField("Title").BeginsWith("[Google]")
        .Or()
        .TextField("Content").Contains("Google")
        .ToString();

This will generate expected CAML:

<Where><Or><Eq><FieldRefName="Email"/><ValueType="Text">support@google.com</Value></Eq><Or><Eq><FieldRefName="Email"/><ValueType="Text">plus@google.com</Value></Eq><Or><BeginsWith><FieldRefName="Title"/><ValueType="Text">[Google]</Value></BeginsWith><Contains><FieldRefName="Content"/><ValueType="Text">Google</Value></Contains></Or></Or></Or></Where>

, which then could be used in SP.CamlQuery.

Very good so far, and have a look at some other examples!

First of all, cannot resist to mention, that CamlBuilder covers almost all Query elements, described at MSDN.

For example, seldom used Membership element:

var caml = camlBuilder.Where()
        .UserField("AssignedTo").EqualToCurrentUser()
        .Or()
        .UserField("AssignedTo").IsInCurrentUserGroups()
        .GroupBy("ProductTitle")
        .OrderBy("Priority").ThenBy("Title")
        .ToString();

This code will generate following CAML:

<Where><Or><Eq><FieldRefName="AssignedTo"/><ValueType="Integer"><UserID/></Value></Eq><MembershipType="CurrentUserGroups"><FieldRefName="AssignedTo"/></Membership></Or></Where><GroupBy><FieldRefName="ProductTitle"/></GroupBy><OrderBy><FieldRefName="Priority"/><FieldRefName="Title"/></OrderBy>

The last example at this section:

    caml = camlBuilder.Where()
        .LookupField("Category").Id().In([2, 3, 10])
        .And()
        .DateField("ExpirationDate").LessThanOrEqualTo(CamlBuilder.CamlValues.Now)
        .OrderByDesc("ExpirationDate")
        .ToString()

As you see, the code is pretty clean and readable. The resulting CAML is much more awkward, especially if you imagine it in javascript strings dress, without indentation and highlighting...

<Where><And><In><FieldRefName="Category"LookupId="True"/><Values><ValueType="Integer">2</Value><ValueType="Integer">3</Value><ValueType="Integer">10</Value></Values></In><Leq><FieldRefName="ExpirationDate"/><ValueType="Date"><Now/></Value></Leq></And></Where><OrderBy><FieldRefName="ExpirationDate"Ascending="False"/></OrderBy>

Usability

CamlBuilder is not only readable and simple, but it also provides rich intellisense and inline documentation:

This will help to catch a bunch of errors when writing code, and surely prevent from typos and misunderstandings, so the benefits could be really magnificient!

Intellisense and inline docs are even better if you use CamlJs with TypeScript.

Contribute

Any contributions are highly appreciated!

New Post: SP.CamlQuery() needs ""

$
0
0
Starting from release 2.5, there are two changes related to this:
  • View element is now supported by CamlJs. So if you write something like this:
new CamlBuilder().View().Query().Where().TextField('foo').Contains('bar').ToString();
, then you will get View & Query elements in the resulting string.
  • .ToCamlQuery() method is added as alternative to the .ToString() finalization. Produces ready-to-use SP.CamlQuery object

New Post: SP.CamlQuery() needs ""

$
0
0
Hi omlin,

Should .ToCamlQuery() method work with SP2013? Because now its not working. For example:
new CamlBuilder().Where().IntegerField('ID').EqualTo(1).ToString()
returns "<Where><Eq><FieldRef Name="ID" /><Value Type="Integer">1</Value></Eq></Where>"
but
new CamlBuilder().Where().IntegerField('ID').EqualTo(1).ToCamlQuery()
returns object where viewXml parameter is wrapped into "<View><Query>":
SP.CamlQuery {$15_1: true, $l_1: "<View><Query><Where><Eq><FieldRef Name="ID" /><Val…e="Integer">1</Value></Eq></Where></Query></View>", $2Q_1: null, $2c_1: null, get_datesInUtc: function…}
As a result this query always returns nothing.

New Post: SP.CamlQuery() needs ""

$
0
0
Sorry, I guess it's my mistake.
We need to use new CamlBuilder().View().Query().Where().IntegerField('ID').EqualTo(1).ToString() or new CamlBuilder().Where().IntegerField('ID').EqualTo(1).ToCamlQuery(), right? It doesn't depend from SP version?

New Post: SP.CamlQuery() needs ""

$
0
0
Hi,

Just to clarify one more time:
  • ToString returns "<Where>..</Where>", if it was created like this: new CamlBuilder().Where()...ToString()
  • ToString returns "<View>..</View>", if it was created like this: new CamlBuilder().View()...ToString()
  • ToCamlQuery always returns "<View>..</View>" in viewXml property of the SP.CamlQuery object, because SP.CamlQuery viewXml property requires View-based query. So if query was created via new CamlBuilder().Where()...ToString(), then it just wraps the result into view.
This doesn't anyhow depend on SharePoint version. ToCamlQuery should work in SP2010 and SP2013; ToString works in SP2007, SP2010 and SP2013.

At least this is how it should work. Bugs happen, you know :)
So if you find that something is not as described, just tell me, and I fix it.

New Post: IncludeTimeValue == True?

$
0
0
Hi Andrey,

Thank you for your great job! Not only for CamlJS project, but for SharePoint work at all!

I have a question regrading IncludeTimeValue CAML setting. Is it possible to generate following CAML using Camljs?
<Geq>
    <FieldRef Name="EndDate" />
    <Value Type="DateTime" IncludeTimeValue="True">
        <Today />
    </Value>
</Geq>
I've tried both
 new CamlBuilder().Where()
.DateField("EndDate").GreaterThanOrEqualTo("{Today}")
.ToString(); 
and
new CamlBuilder().Where()
.DateTimeField("EndDate").GreaterThanOrEqualTo("{Today}")
.ToString();
but IncludeTimeValue is always false or absent.
Can you please help?

Source code checked in, #dc33253cbb2ffd4998d93e5bb100406f1bac8086

$
0
0
IncludeTimeValue bug fixed, thanks sKai for reporting it!

New Post: IncludeTimeValue == True?

$
0
0
Hi,

Thanks for your feedback, really appreciated!

DateTimeField should have done the trick, but it seems this is a bug. Based on documentation on MSDN, IncludeTimeValue is FALSE by default, though CamlJs seems to think that TRUE is the default value, and this is why the problem happens. Also I just performed some testing using camljs-console and confirmed that time value is not respected if IncludeTimeValue is absent.

Fortunately, this is very easy to fix.

I just applied the fix and pushed the changes to CodePlex.
Please download and try the latest version of camljs.js from Source Code tab.

I'll update the release package, Nuget and Chrome WebStore ASAP.

New Post: IncludeTimeValue == True?

$
0
0
Thanks for the fast fix!

But does your fix mean, that every <Value> tag will have IncludeTimeValue=True by default?

New Post: IncludeTimeValue == True?

$
0
0
No, at least intention is that if you use DateTimeField, then Value will have IncludeTimeValue = True, all other cases - no.
Not really tested the fix though, some silly mistake is possible of course.

New Post: Any way to return only the VewFields

$
0
0
Is there a way to define ViewFields and have them returned as a string?
I'm experimenting with this library and the use of SOAP operations (SP2007)... I would like to get back the string of ViewFields... but looks like the only way is to define .View().. but that returns back a <View> along with it.

I guess I could eval the string in JQuery and then select the ViewFields element only... but was trying to avoid that...

Thanks
/Paul.

New Post: Any way to return only the VewFields

$
0
0
You're right, currently it's not possible to have ViewFields alone, only within a View.

But actually I think separate ViewFields tag generation might be handy when using camljs with SPServices. I'll see if I can squeeze some time and implement this one. Thanks for the good idea!

New Post: Any way to return only the VewFields

$
0
0
Np...
Anyone else who might find this thread looking for the same thing - here is my current workaround:
                CAMLViewFields  : (function(){
                    var reEx = (/\<ViewFields\>.*\<\/ViewFields\>/i).exec(
                            new CamlBuilder().View(["Tile", "Expires", "When"]).ToString()
                        );
                    if (reEx) {
                        return reEx[0];
                    }
                    return "<ViewFields></ViewFields>"
                }())

Source code checked in, #018d7dade0504151debb7106ad5413de04996359

$
0
0
fix for IncludeTimeValue="True" that appeared for all value types except "Date" rather than only for "DateTime" value type. also added a possibility to generate ViewFields separately, which can be handy if using camljs library with libs/code that uses SharePoint Web Services (e.g. SPServices). tests passed.
Viewing all 199 articles
Browse latest View live


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>