2

I have a CAML Query with range between two dates and one text from DropDownList.

Where I'm wrong in this query:

myquery.Query = "
    <Where>
        <And>
            <FieldRef Name='Company'/><Value Type='Text'>" + ddComFilter.SelectedItem.Text + "</Value>
            <Gt>
                <FieldRef Name='DateOfCompletion'/><Value Type='DateTime'>" + dtFrom.SelectedDate.ToString("s") + "</Value>
            </Gt>
            <Lt>
                <FieldRef Name='DateOfCompletion'/><Value Type='DateTime'>" + dtTo.SelectedDate.ToString("s") + "</Value>
            </Lt>
        </And>
    </Where>";

PS--> This is working :

<And>
            <Gt>
                <FieldRef Name='DateOfCompletion'/><Value Type='DateTime'>" + dtFrom.SelectedDate.ToString("s") + "</Value>
            </Gt>
            <Lt>
                <FieldRef Name='DateOfCompletion'/><Value Type='DateTime'>" + dtTo.SelectedDate.ToString("s") + "</Value>
            </Lt>
        </And>
4

You can not have more than 2 statements in every <And> / <Or> tag. Also you seem to be missing an <Eq> (or whatever you need) around your first <FieldRef> tag.

So instead do this

myquery.Query = "
    <Where>
        <And>
            <Eq>
                <FieldRef Name='Company'/><Value Type='Text'>" + ddComFilter.SelectedItem.Text + "</Value>
            </Eq>
            <And>
                <Gt>
                    <FieldRef Name='DateOfCompletion'/><Value Type='DateTime'>" + dtFrom.SelectedDate.ToString("s") + "</Value>
                </Gt>
                <Lt>
                    <FieldRef Name='DateOfCompletion'/><Value Type='DateTime'>" + dtTo.SelectedDate.ToString("s") + "</Value>
                </Lt>
            </And>
        </And>
    </Where>";
  • I know about it, he wanted to go 7 minutes.. Best regards – Goshky Apr 28 '16 at 14:05
1

An "and" can only have 2 child nodes, thus you should amend your CAML:

myquery.Query = "
<Where>
<And>
    <Eq>
    <FieldRef Name='Company'/><Value Type='Text'>" + ddComFilter.SelectedItem.Text + "</Value>
    </Eq>
</And>
    <And>
        <Gt>
            <FieldRef Name='DateOfCompletion'/><Value Type='DateTime'>" + dtFrom.SelectedDate.ToString("s") + "</Value>
        </Gt>
        <Lt>
            <FieldRef Name='DateOfCompletion'/><Value Type='DateTime'>" + dtTo.SelectedDate.ToString("s") + "</Value>
        </Lt>
    </And>
</Where>";
  • 1
    You're missing the outer <And> – Robert Lindgren Apr 28 '16 at 13:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.