Create query from array

Nov 6, 2013 at 11:21 AM
Hi,

How can I create a query from an array?

For example if I need to use the <Includes>, than Any() cannot plays, I need to build <Or> blocks. How can I do this?

BTW, thanks for this excellent tool, It saves me a lot of work! :)
Coordinator
Nov 6, 2013 at 2:55 PM
Edited Nov 6, 2013 at 2:56 PM
Hi,

Thanks for the feedback!

If you cannot use In, that accepts array, you have to use Any.
Any actually builds the Or blocks internally, so you don't need to care about it.
Unfortunately, Any doesn't accept arrays at the moment (I'm going to improve it in the next release), so you have to call it using apply :(

It is a little tricky because you have to pass the right context, otherwise it wouldn't work.
I had an example somewhere, I'll try to find it and post it here ASAP.
Coordinator
Nov 6, 2013 at 9:58 PM
Ok, so here is the example I made for adjacent thread, I think it is illustrative enough.

In this example there are two arrays, categories and purposes.

And I'm producing query

(Category=category[0] or Category=category[1] or ... or Category=category[N]) AND (Purpose=purpose[0] or Purpose=purpose[1] or .. or Purpose=purpose[M])

So basically it means creating query from two arrays.
var categories = ["Platform Support", "Research and Strategy"];
var purposes = ["Application and User Lists", "How To", "Support Information"];

var categoriesExpressions = [];
for (var i = 0; i < categories.length; i++) {
    categoriesExpressions.push(
        CamlBuilder.Expression().TextField("ContentCategory").EqualTo(categories[i])
    );
}
var purposesExpressions = [];
for (var i = 0; i < purposes.length; i++) {
    purposesExpressions.push(
        CamlBuilder.Expression().TextField("ContentPurpose").EqualTo(purposes[i])
    );
}

var categoriesBuilder = CamlBuilder.Expression();
var purposesBuilder = CamlBuilder.Expression();

var caml = new CamlBuilder().Where().All(
        categoriesBuilder.Any.apply(categoriesBuilder, categoriesExpressions), 
        purposesBuilder.Any.apply(purposesBuilder, purposesExpressions)
).ToString();
Result:
<Where>
    <And>
        <Or>
            <Eq>
                <FieldRef Name="ContentCategory"/>
                <Value Type="Text">Platform Support</Value>
            </Eq>
            <Eq>
                <FieldRef Name="ContentCategory"/>
                <Value Type="Text">Research and Strategy</Value>
            </Eq>
        </Or>
        <Or>
            <Eq>
                <FieldRef Name="ContentPurpose"/>
                <Value Type="Text">Application and User Lists</Value>
            </Eq>
            <Or>
                <Eq>
                    <FieldRef Name="ContentPurpose"/>
                    <Value Type="Text">How To</Value>
                </Eq>
                <Eq>
                    <FieldRef Name="ContentPurpose"/>
                    <Value Type="Text">Support Information</Value>
                </Eq>
            </Or>
        </Or>
    </And>
</Where>
Marked as answer by omlin on 11/8/2013 at 3:36 PM
Nov 7, 2013 at 2:59 PM
Edited Nov 7, 2013 at 3:19 PM
Thank you, it is working! :)
Nov 8, 2013 at 8:11 PM
Can i use this CAML builder in javascript? if yes how?
Coordinator
Nov 8, 2013 at 8:20 PM
Edited Nov 8, 2013 at 10:38 PM
Where else if not in JS? Sure you can.

Download the camljs.js file and use examples from the start page.

If you will face some particular difficulties, please create a separate discussion thread and explain them there.
Thanks!
Nov 11, 2013 at 2:01 PM
ok created a new thread https://camljs.codeplex.com/discussions/467956 any help would be appreciated.