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

Updated Release: Release 2.6

$
0
0

New features

  1. Now it is possible to use CamlJs for modifying existing CAML queries:
    • FromXml method will create a CamlBuilder object from existing CAML string
    • ReplaceWhere method allows to replace <Where> clause with one generated by CamlJs
    • ModifyWhere().AppendAnd() will add new conditions to existing query using "And" operator
    • ModifyWhere().AppendOr() will add new conditions to existing query using "Or" operator

Updated Release: Release 2.6 (Oct 07, 2015)

$
0
0

New features

  1. Now it is possible to use CamlJs for modifying existing CAML queries:
    • FromXml method will create a CamlBuilder object from existing CAML string
    • ReplaceWhere method allows to replace <Where> clause with one generated by CamlJs
    • ModifyWhere().AppendAnd() will add new conditions to existing query using "And" operator
    • ModifyWhere().AppendOr() will add new conditions to existing query using "Or" operator

Released: Release 2.6 (Oct 07, 2015)

$
0
0

Changes

  1. Now it is possible to use CamlJs for modifying existing CAML queries:
    • FromXml method will create a CamlBuilder object from existing CAML string
    • ReplaceWhere method allows to replace <Where> clause with one generated by CamlJs
    • ModifyWhere().AppendAnd() will add new conditions to existing query using "And" operator
    • ModifyWhere().AppendOr() will add new conditions to existing query using "Or" operator
  2. Added Query().GroupBy(...) and Query().OrderBy(...)

Updated Release: Release 2.6 (Oct 07, 2015)

$
0
0

Changes

  1. Now it is possible to use CamlJs for modifying existing CAML queries:
    • FromXml method will create a CamlBuilder object from existing CAML string
    • ReplaceWhere method allows to replace <Where> clause with one generated by CamlJs
    • ModifyWhere().AppendAnd() will add new conditions to existing query using "And" operator
    • ModifyWhere().AppendOr() will add new conditions to existing query using "Or" operator
  2. Added Query().GroupBy(...) and Query().OrderBy(...)

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.6

  1. Now it is possible to use CamlJs for modifying existing CAML queries:
    • FromXml method will create a CamlBuilder object from existing CAML string
    • ReplaceWhere method allows to replace <Where> clause with one generated by CamlJs
    • ModifyWhere().AppendAnd() will add new conditions to existing query using "And" operator
    • ModifyWhere().AppendOr() will add new conditions to existing query using "Or" operator

    Example:

  2. Added Query().GroupBy(...) and Query().OrderBy(...)

 

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: Code not returning expected query

$
0
0
Hi,

As far as I remember And()-s and Or()-s are evaluated in right-to-left order, so probably to fix your problem you just need to change order corresponding your logic.

This particular query can actually simplified like this:
var query = new CamlBuilder().Where().TextField("ID").In(arrayName).And().TextField("HighRiskProtocol").Contains("Test").ToString()
Also, for building more complex dynamic expressions, you can use CamlBuilder.Expression.
Example:
var expressions = [];
for (var i=0;i<arrayName.length;i++)
{
  expressions.push(CamlBuilder.Expression().Where().TextField("ID").EqualTo(arrayName[i]));
}
var query = new CamlBuilder().Where().Any(expressions).And().TextField("HighRiskProtocol").Contains("Test").ToString(); 
P.S. Terribly sorry for the very late answer, for some time codeplex had problems sending out emails, I think that's why I completely missed your question.

New Post: Group="True" in CamlJS

$
0
0
Hi Martin,

Thanks, appreciate your feedback.
No, Group attribute hasn't made into camljs because I performed some testing and haven't noticed any difference between queries with this attribute and without it. Also I cannot find any official documentation regarding this attribute.

It gets generated in SharePoint Designer, and actually my guess is that SharePoint Designer uses it for displaying this query in it's UI, but when actually executing the query, it is not needed.

P.S. Sorry for the very late answer, codeplex had some problems sending out emails, so I haven't got any notification about your post and completely missed it.

Best,
Andrei

New Post: Can I set a RowLimit?

$
0
0
I cannot find any documentation on setting a RowLimit.

Is this supported?

New Post: Can I set a RowLimit?

$
0
0
Never mind.

I have just found it. I had it set at the wrong level (after my .Query()). I moved it to after the .View() and before .Query() and it works.

New Post: Can I set a RowLimit?

$
0
0
Glad you found it! :)
Btw have a look at CamlJs-Console, it has intellisense, so that you can easily explore the API.

New Post: Can I set a RowLimit?

$
0
0
Fab.

I'll take a look at the console.. that will help. I am writing a .txt plugin to be surfaced from a Content Editor Webpart so I get zero intenseness :-(

Great tool.. keep up the good work

New Post: Progressively Build up my CAML

$
0
0
So, I have 4 multi-select dropdown boxes on my page where users can choose filter options...

I'll need to build up some complex CAML from these for my list item selector, but I need to progressively build it up.

For example, if dropdown box 1 has selections, then add a Where Clause ,and if there are more than one selected, OR them.

Same with dropdown 2... but then we need to AND the different dropdown boxed, etc. i.e:

(dd1op1 OR dd1op2 OR dd1op5) AND (dd2op2 OR dd2op3) AND (dd3op6)

Can I do this programmatically? Once I have created my CAML using the builder I can't see how to add new clauses in. There is the ModifyAdd.. but I am starting from no Where items (just a view with a row limit) but want to push the Where clauses in as appropriate.

I hope that makes sense and isn't too confusing.

New Post: Progressively Build up my CAML

$
0
0
Hi,

If I got you right, it's just a nested dynamic query. This can be solved with CamlBuilder.Expression.

Let's say you prepared arrays with values:
var dd1_values = ['dd1op1','dd1op2'];
var dd2_values = ['dd2op1'];
var dd3_values = ['dd3op1','dd3op2','dd3op3','dd3op4'];
Then you can build expressions from those values, like this:
var dd1_expr_array = [];
for (var i=0;i<dd1_values.length.i++)
{
  dd1_expr_array.push( CamlBuilder.Expression().TextField("Category1").EqualTo(dd1_values[i]) );
}
And so forth for all drop downs.

Now let's join them:
var all_expr_array = [];
if (dd1_expr_array.length>0)
 all_expr_array.push(dd1_expr_array);
if (dd2_expr_array.length>0)
 all_expr_array.push(dd2_expr_array);
if (dd3_expr_array.length>0)
 all_expr_array.push(dd3_expr_array);
Then, form the final query:
var query = new CamlBuilder().Where().All(all_expr_array).ToString();
console.log(query);
Something like this.

P.S. Of course, if your values are integers (i.e. IDs), then you'd better use In operator, like this:
var dd1_expr = CamlBuilder.Expression().TextField("Category1").In(dd1_values[i]);

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.6

  1. Now it is possible to use CamlJs for modifying existing CAML queries:
    • FromXml method will create a CamlBuilder object from existing CAML string
    • ReplaceWhere method allows to replace <Where> clause with one generated by CamlJs
    • ModifyWhere().AppendAnd() will add new conditions to existing query using "And" operator
    • ModifyWhere().AppendOr() will add new conditions to existing query using "Or" operator

    Example:

  2. Added Query().GroupBy(...) and Query().OrderBy(...)

 

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: Progressively Build up my CAML

$
0
0
Incredible.

Thank you so much. This works like a dream,

Please keep up the good work with this tool. It just saved me a ton of time.

Created Unassigned: Syntax error using .ModifyWhere [7]

$
0
0
Syntax error on line 367 : xmlDoc = parser.parseFromString(this.xml, "text/xml");

Commented Unassigned: Syntax error using .ModifyWhere [7]

$
0
0
Syntax error on line 367 : xmlDoc = parser.parseFromString(this.xml, "text/xml");
Comments: This appears to be an Internet Explorer 11 issue from a google search.

Commented Unassigned: Syntax error using .ModifyWhere [7]

$
0
0
Syntax error on line 367 : xmlDoc = parser.parseFromString(this.xml, "text/xml");
Comments: Well, IE11 is quite popular among SharePoint users, so this is quite relevant. Thanks for reporting, I'll look into this!

Reviewed: Release 2.6 (Jan 02, 2016)

$
0
0
Rated 4 Stars (out of 5) - Without doubt the easiest way to dynamically build a Caml query!

New Post: IncludeTimeValue == True?

$
0
0
I've played with this functionality quite a bit over the past few weeks and it works as advertised. However, users really need to be aware upfront of this subtly as DateTime field comparisons are tricky enough without tripping over the inability to change IncludeTimeValue.

My original hack to fix this issue was to replace 'True' with 'False' using javascripts inbuilt replace string functionality . However, I've since discovered that using a Date field as the comparator (ie. <Value Type="Date">) appears to work for DateTime fields as if you used <Value Type="DateTime" IncludeTimeValue="False">.

It would be nice to have the ability to tell CamlJS what you want the value of IncludeTimeValue to be. That way you could equate the Caml string to the actual fields you used rather than implementing a hack.

Does the job either way though... :-)
Viewing all 199 articles
Browse latest View live


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