views:

1571

answers:

2

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!

A: 

First off, a common mistake in CAML is to not use the internal name of a field... try using the

list.Fields["Display Name"].InternalName

Secondly, use the SPUtility's date utility

Microsoft.SharePoint.Utilities.SPUtility.CreateISO8601DateTimeFromSystemDateTime(System.DateTime.Now)
UJ
Thanks, but this is done through the schema.xml, not through code. I can't use CreateISO8601DateTimeFromSystemDateTime. As for the Display Name, the CAML inside a schema.xml uses the internal name. It's the only thing that works for view fields, field refs, etc.
Hugo Migneron
"First off, a common mistake in CAML is to not use the internal name of a field..." - I wonder why? Display name can change - I can even change it in UI, but InternalName cannot change.
Janis Veinbergs
+1  A: 

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>
Colin
Thanks a lot Colin, I wasn't familiar with the DataFormWebPart. So if I understand correctly, I need a DFWP in the form where I push my view (so I have the DFWP and the ListView Web Part on the same form). And then I add the ParameterBinding in the schema.xml of my list definition? Is that correct? Is so, where do I need to add the ParameterBinding? I'd imagine that the query will just return an empty result when the control isn't there or if the Parameter isn't seen, which is what i'm getting when I test, which is why I wonder where I have to place the parameterBinding... Thanks a lot!
Hugo Migneron
Using SharePoint Designer, create ea new ASPX page (anywhere in the site). Then open the Datasources tool window and drag your list onto the page. It will Generate an SPDatasource and a DFWP using that DataSource. You cna then in Code alter the DataSource's SelectCommand and in design mode specify filters (by using the DFWP context menu (just like in Visual Studio). Find more info here: http://www.docstoc.com/docs/4481600/Getting-to-know-the-Data-Form-Web-Part-Series
Colin