SharePoint List View – DateTime in the Query String

camlsharepointwss

I am writing a view for a list of events in SharePoint (Schema.xml) and I want to filter the results according to a DateTime (i.e. only display the events that start between 2 dates)

Normally I would use a CAML Query like this one for example :

         <Where>
            <And>
              <Geq>
                <FieldRef Name="Event_x0020_Start_x0020_Date" />
                <Value Type=”DateTime”>2009-10-10T10:00:00Z</Value>
              </Geq>
              <Leq>
                <FieldRef Name="Event_x0020_Start_x0020_Date" />
                <Value Type=”DateTime”>2009-11-10T10:00:00Z</Value>
              </Leq>
            </And>
          </Where>

However, in this case, the dates that I want to compare to are not available directly in a field, I have to fetch them from the Query String.

I tried using

        <Value Type="DateTime">
          <GetVar Scope="Request" Name="Start" />
        </Value>
        <Value Type="DateTime">
          <GetVar Scope="Request" Name="End" />
        </Value>

where Start and End are 2 dates in the Query String (I tried every date format, with and without the Type="DateTime") but I always get empty results. The query works fine when I hardcode my dates (with say 2009-10-10T10:00:00Z).

I have control over what I send in the Query String so I can change it if there is another way.

So is there a way to get a DateTime format in the query string? If not, do I have other options?

Thanks!

Best Answer

Have you tried adding a custom page and then adding a DataFormWebPart (DFWP) to it? That in turn would allow you to shape your CAMl query in the SelectCommand of the SPDatasource used by the DFWP, using actual ASP.NET Calendar Controls to be used as parameters, specified in the SPDataSource's parameters. Use Control(ID, PROPERTYTOUSEINCAML) in the parameterbindings of the spdatasource.

i.e.:

<ParameterBinding Name="StartDate" Location="Control(calStart, SelectedDate)" DefaultValue="01-01-2009"/>
<ParameterBinding Name="EndDate" Location="Control(calEnd, SelectedDate)" DefaultValue="01-01-2009"/>

then have the SelectCommand's CAML be something like:

<Where>
  <And>
    <Geq>
      <FieldRef Name="Event_x0020_Start_x0020_Date" />
      <Value Type=”DateTime”>{StartDateParameter}</Value>
    </Geq>
    <Leq>
      <FieldRef Name="Event_x0020_Start_x0020_Date" />
      <Value Type=”DateTime”>{EndDateParameter}</Value>
    </Leq>
  </And>
</Where>
Related Topic