AND/OR Grouping Support

Nov 6, 2013 at 9:34 PM
So far this is script is a huge timesaver thanks a ton for your effort!

My question is whether or not Caml Builder can support and/or groups?
SharePoint Example:

<Query>
<Where>
    <And>
        <Or Group="true">
            <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 Group="true">
            <Or>
                <Eq>
                    <FieldRef Name="ContentPurpose"/>
                    <Value Type="Text">How To</Value>
                </Eq>
                <Eq>
                    <FieldRef Name="ContentPurpose"/>
                    <Value Type="Text">Support Information</Value>
                </Eq>
            </Or>
            <Eq>
                <FieldRef Name="ContentPurpose"/>
                <Value Type="Text">Application and User Lists</Value>
            </Eq>
        </Or>
    </And>
</Where>
</Query>

Which (from my understanding) is basically OR between items within the same column with AND between the two columns
"Return all items in the list/library that match [( A OR B ) AND ( C OR D OR E )]"

My intention is to have the user create the query via a custom UI and clicking a submit button that will store the terms into variables and convert them into a CAML query which will then be stored inside a cookie as a string. The page will refresh once the cookie has been saved and the web part will load the cookie as its CAML query.
Nov 6, 2013 at 10:40 PM
Edited Nov 6, 2013 at 10:49 PM
Hi,

Thanks for your feedback, this is really important for me!! :)

Honestly this is first time I hear about Group attribute for "Or" tags. I don't have any idea what it does. Are you sure it is necessary there?
This attribute isn't supported by CamlJs.

But if we ignore this Group attribute, it seems the query in your example can be easily generated using CamlJs:
var caml = new CamlBuilder().Where()
    .All(
        CamlBuilder.Expression().Any(
            CamlBuilder.Expression().TextField("ContentCategory")
                .EqualTo("Platform Support"),
            CamlBuilder.Expression().TextField("ContentCategory")
                .EqualTo("Research and Strategy")
        ),
        CamlBuilder.Expression().Any(
            CamlBuilder.Expression().TextField("ContentPurpose")
                .EqualTo("How To"),
            CamlBuilder.Expression().TextField("ContentPurpose")
                .EqualTo("Support Information"),
            CamlBuilder.Expression().TextField("ContentPurpose")
                .EqualTo("Application and User Lists")
        )
    )
    .ToString();
If number of items can vary, then you will need something like this:
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();
Note: "+" stands for the plus sign, silly codeplex cannot process it correctly for some reason :(


It looks a bit clumsy since Any and All methods don't support array parameters at the moment (this will certainly be fixed in the next release). This is why I have to use apply here.

But it works fine and I would say that it is still quite readable, so hopefully this will serve you :)

Best,
Andrei