SharePoint CAML Query to Filter Data By Date

68 Views Asked by At

I am working on SharePoint application using Visual Studio (C#).

There is requirement to fetch the data by filtering date from DateColumn1, if the DateColumn1 is null/empty it should filter DateColumn2.

It should not filter both the column at a time.

Parameter1: 01-01-2023 Parameter2: 02-01-2023

DateColumn1 DateColumn1 OtherColumn
01-01-2023 04-01-2023 Some Text
01-01-2023 Some Text
08-12-2022 01-01-2023 Some Text

In my case, query should return only 2 records i.e. Row 1 & Row 2. It should ignore Row3 as DateColumn1 is not null.

<Where>
<Contains>
    <FieldRef Name='DocTypePath'/><Value Type='Text'>misc</Value></Contains>
<Or>
<And>
    <Geq><FieldRef Name='DateColumn1'/><Value IncludeTimeValue='FALSE' Type='DateTime'>2023-01-01</Value></Geq>
    <Leq><FieldRef Name='DateColumn1'/><Value IncludeTimeValue='FALSE' Type='DateTime'>2023-01-02</Value></Leq>
</And>
<And>
    <Geq><FieldRef Name='DateColumn2'/><Value IncludeTimeValue='FALSE' Type='DateTime'>2023-01-01</Value></Geq>
    <Leq><FieldRef Name='DateColumn2'/><Value IncludeTimeValue='FALSE' Type='DateTime'>2023-01-02</Value></Leq>
</And>
</Or>
</Where>
<OrderBy>
<FieldRef Name = 'Created' Ascending = 'False'/> </OrderBy>

The above query is filtering both the columns at a time. In my case I have to filter 2nd column only when the DateColumn1 is empty.

0

There are 0 best solutions below