Question

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>
Was it helpful?

Solution

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>";

OTHER TIPS

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>";
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top