CAML Query Search Between Two Date Ranges : SharePoint


SPSite site = new SPSite(“http://localhost:3001”);
SPWeb web = site.OpenWeb(“MyWeb”);
SPList list = web.Lists[“MyList”];

//RequestStartDate & RequestEndDate will be passed in mm/DD/yyyy format.
//They can extend  to work with time also.

string startDate = (SPUtility.CreateISO8601DateTimeFromSystemDateTime(Convert.ToDateTime(this.RequestStartDate)));
string endDate = (SPUtility.CreateISO8601DateTimeFromSystemDateTime(Convert.ToDateTime(this.RequestEndDate)));

SPQuery query=new SPQuery();
query.Query = “<Where><And><Geq><FieldRef Name=’Date_x0020_of_x0020_Meeting’ /><Value Type=’DateTime’>” + startDate + “</Value></Geq><Leq><FieldRef Name=’Date_x0020_of_x0020_Meeting’ /><Value Type=’DateTime’>” + endDate + “</Value></Leq></And></Where></Query>”;
DataTable dt = list.GetItems(query).GetDataTable();

This will return the records between two dates including time (both days will also include.). If you dont supply time it will take default time to search.

Advertisements

5 thoughts on “CAML Query Search Between Two Date Ranges : SharePoint

  1. use this caml query for date function in shell script

    $now = (Get-Date).AddDays(-1);
    $lastrun_converted = [microsoft.sharepoint.utilities.sputility]::CreateISO8601DateTimeFromSystemDateTime($now);

    write-host $lastrun_converted;

    $camlsource = “$lastrun_converted”

    #$query=new-object Microsoft.SharePoint.SPQuery

    $queryfromsource=new-object Microsoft.SharePoint.SPQuery

    #$query.ViewAttributes = “Scope=`”Recursive`””

    $queryfromsource.ViewAttributes = “Scope=`”Recursive`””

    $queryfromsource.Query=$camlsource

    #$query.Query=$camlsource
    #$result = $list.GetItems($query)

    enjoy..
    Deva
    Bangalore

Leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s