0

I have a SharePoint list. It has two columns Start date and End date. I need to query and get data if (start date + 7 days> End date). Through CAML builder its not possible to have a SharePoint column on value node and build the query. Any idea? I have tried below. But not working.

<Query>
   <Where>
      <Eq>
         <FieldRef Name='EndDate' />
         <Value IncludeTimeValue='TRUE' Type='DateTime'><StartDate+7/></Value>
      </Eq>
   </Where>
</Query>
Sivakumar Piratheeban
  • 493
  • 4
  • 11
  • 39

3 Answers3

1

You can not compare two field of item to each other in CAML query. You can either create computed field and do the compare in it or you can use LINQ. Something like this:

SPList tasks = SPContext.Current.Web.Lists["tasks"];
var ts = from t in tasks.Items.OfType<SPListItem>() where t["DueDate"] == null || (DateTime)t["Modified"] > (DateTime)t["DueDate"] select t;
Jan Vanek
  • 889
  • 1
  • 6
  • 8
1

I would recommend creating a calculated column to calculate the difference between the start and end date:

=DATEDIF([Created], [EndDate],"d")

and then in your camlquery filter by greater or equal than 7 days

<Query>
   <Where>
      <Geq>
         <FieldRef Name="DateDiff"/>
         <Value IncludeTimeValue='TRUE' Type='Number'>7<Value>
      </Geq>
   </Where>
</Query>
Mike
  • 789
  • 8
  • 15