SPQuery with datetime field hi,<br><br>      i need to show created, created by and document name into gridview based on created date.<br><br>for ex: I have two textbox one for &quot;from date&quot; and other for &quot;to date&quot; and a button. when user click on the button what are the documents available in document library between the two date. <br>i have tried this way,<br><font size=2> <p></font><font color="#2b91af" size=2>SPQuery</font><font size=2> oQuery = </font><font color="#0000ff" size=2>new</font> <font color="#2b91af" size=2>SPQuery</font><font size=2>();<br></font><font size=2>oQuery.ViewFields = </font><font color="#a31515" size=2>&quot;&lt;FieldRef Name='Created'/&gt;&quot;</font><font size=2> + </font><font color="#a31515" size=2>&quot;&lt;FieldRef Name='FileLeafRef'/&gt;&quot;</font><font size=2> + </font><font color="#a31515" size=2>&quot;&lt;FieldRef Name='Author'/&gt;&quot;</font><font size=2>;<br></font><font color="#2b91af" size=2>DateTime</font><font size=2> strFromDate = </font><font color="#2b91af" size=2>Convert</font><font size=2>.ToDateTime(txtFromDate.Text);<br></font><font color="#2b91af" size=2>DateTime</font><font size=2> strToDate = </font><font color="#2b91af" size=2>Convert</font><font size=2>.ToDateTime(txtToDate.Text);<br></font><font size=2>oQuery.Query = </font><font color="#a31515" size=2>&quot;&lt;Where&gt;&lt;FieldRef Name='Created'&gt;&lt;Value Type=\&quot;DateTime\&quot; StorageTZ='TRUE'&gt;&quot;</font><font size=2> + strFromDate + </font><font color="#a31515" size=2>&quot;&lt;/Value&gt;&lt;FieldRef Name='Created'&gt;&lt;Value Type=\&quot;DateTime\&quot; StorageTZ='TRUE'&gt;&quot;</font><font size=2> + strToDate + </font><font color="#a31515" size=2>&quot;&lt;/Value&gt;&lt;/Where&gt;&quot;</font><font size=2>;<br></font><font color="#2b91af" size=2>SPListItemCollection</font><font size=2> items = splist.GetItems(oQuery);<br></font><font color="#2b91af" size=2>DataTable</font><font size=2> dt = items.GetDataTable();<br></font><font color="#0000ff" size=2>return</font><font size=2> dt;<br>i am new to this technology.<br></p></font><br>i don't know how to use the spquery for this scenario.<br><br>please anyone can help me.......<br>thanks in advace........<br><br><font color="#2b91af" size=2> <p></font><font size=2><font size=2><br> </p></font></font>© 2009 Microsoft Corporation. All rights reserved.Fri, 03 Apr 2009 14:58:43 Z076c57b1-a780-4b86-b00b-849fa8789318http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/076c57b1-a780-4b86-b00b-849fa8789318#076c57b1-a780-4b86-b00b-849fa8789318http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/076c57b1-a780-4b86-b00b-849fa8789318#076c57b1-a780-4b86-b00b-849fa8789318prabhukumardhttp://social.msdn.microsoft.com/Profile/en-US/?user=prabhukumardSPQuery with datetime field hi,<br><br>      i need to show created, created by and document name into gridview based on created date.<br><br>for ex: I have two textbox one for &quot;from date&quot; and other for &quot;to date&quot; and a button. when user click on the button what are the documents available in document library between the two date. <br>i have tried this way,<br><font size=2> <p></font><font color="#2b91af" size=2>SPQuery</font><font size=2> oQuery = </font><font color="#0000ff" size=2>new</font> <font color="#2b91af" size=2>SPQuery</font><font size=2>();<br></font><font size=2>oQuery.ViewFields = </font><font color="#a31515" size=2>&quot;&lt;FieldRef Name='Created'/&gt;&quot;</font><font size=2> + </font><font color="#a31515" size=2>&quot;&lt;FieldRef Name='FileLeafRef'/&gt;&quot;</font><font size=2> + </font><font color="#a31515" size=2>&quot;&lt;FieldRef Name='Author'/&gt;&quot;</font><font size=2>;<br></font><font color="#2b91af" size=2>DateTime</font><font size=2> strFromDate = </font><font color="#2b91af" size=2>Convert</font><font size=2>.ToDateTime(txtFromDate.Text);<br></font><font color="#2b91af" size=2>DateTime</font><font size=2> strToDate = </font><font color="#2b91af" size=2>Convert</font><font size=2>.ToDateTime(txtToDate.Text);<br></font><font size=2>oQuery.Query = </font><font color="#a31515" size=2>&quot;&lt;Where&gt;&lt;FieldRef Name='Created'&gt;&lt;Value Type=\&quot;DateTime\&quot; StorageTZ='TRUE'&gt;&quot;</font><font size=2> + strFromDate + </font><font color="#a31515" size=2>&quot;&lt;/Value&gt;&lt;FieldRef Name='Created'&gt;&lt;Value Type=\&quot;DateTime\&quot; StorageTZ='TRUE'&gt;&quot;</font><font size=2> + strToDate + </font><font color="#a31515" size=2>&quot;&lt;/Value&gt;&lt;/Where&gt;&quot;</font><font size=2>;<br></font><font color="#2b91af" size=2>SPListItemCollection</font><font size=2> items = splist.GetItems(oQuery);<br></font><font color="#2b91af" size=2>DataTable</font><font size=2> dt = items.GetDataTable();<br></font><font color="#0000ff" size=2>return</font><font size=2> dt;<br>i am new to this technology.<br></p></font><br>i don't know how to use the spquery for this scenario.<br><br>please anyone can help me.......<br>thanks in advace........<br><br><font color="#2b91af" size=2> <p></font><font size=2><font size=2><br> </p></font></font>Wed, 30 Jul 2008 06:35:03 Z2008-07-30T06:35:03Zhttp://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/076c57b1-a780-4b86-b00b-849fa8789318#178045f6-ef3d-434e-b1ab-5a35c8849894http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/076c57b1-a780-4b86-b00b-849fa8789318#178045f6-ef3d-434e-b1ab-5a35c8849894Laxmikant Rathihttp://social.msdn.microsoft.com/Profile/en-US/?user=Laxmikant%20RathiSPQuery with datetime field you need to use AND condition and the &quot;less than&quot; and &quot;greater than&quot; operators to get the result:<br><font size=2> <p>your query should look something like this : <br></font><font color="#0000ff" size=2><br></p> <div style="border-right:#7f9db9 1px solid;border-top:#7f9db9 1px solid;font-size:11px;overflow:auto;border-left:#7f9db9 1px solid;line-height:100%! important;border-bottom:#7f9db9 1px solid;font-family:Courier New;background-color:white"> <table style="border-top-width:0px;border-left-width:0px;margin:2px 0px;width:99%;border-bottom:#eee 0px solid;border-collapse:collapse;background-color:#fff;border-right-width:0px" cellspacing=0 cellpadding=0> <colgroup> <col style="padding-left:10px;font-size:11px;border-bottom:#f7f7f7 1px solid;font-family:Courier New;white-space:nowrap"> <tbody> <tr> <td><font style="font-size:11px">string </font><font style="color:red">createdBeforeQuery</font><font style="font-size:11px"> = </font><font style="color:blue">&quot;&lt;Leq&gt;&lt;FieldRef Name=\'Created\'/&gt;&lt;Value Type=\'DateTime\'&gt;&quot;</font><font style="font-size:11px"> + strToDate.ToString(&quot;s&quot;) + &quot;</font><font style="color:blue">&lt;/</font><font style="font-size:11px">Value</font><font style="color:blue">&gt;</font><font style="font-size:11px">&lt;/</font><font style="font-weight:bold;color:black">Leq</font><font style="font-size:11px">&gt;&quot;;  </font></td></tr> <tr> <td style="background-color:#f7f7f7"> </td></tr> <tr> <td>string </font><font style="color:red">createdAfterQuery</font><font style="font-size:11px"> = </font><font style="color:blue">&quot;&lt;Geq&gt;&lt;FieldRef Name=\'Created\'/&gt;&lt;Value Type=\'DateTime\'&gt;&quot;</font><font style="font-size:11px"> + strFromDate.ToString(&quot;s&quot;) + &quot;</font><font style="color:blue">&lt;/</font><font style="font-size:11px">Value</font><font style="color:blue">&gt;</font><font style="font-size:11px">&lt;/</font><font style="font-weight:bold;color:black">Geq</font><font style="font-size:11px">&gt;&quot;;  </font></td></tr> <tr> <td style="background-color:#f7f7f7"> </td></tr> <tr> <td> </td></tr> <tr> <td style="background-color:#f7f7f7"></font><font style="color:red">oQuery.Query</font><font style="font-size:11px"> = </font><font style="color:blue">&quot;&lt;Where&gt;&lt;And&gt;&quot;</font><font style="font-size:11px"> + createdBeforeQuery + createdAfterQuery + &quot;</font><font style="color:blue">&lt;/</font><font style="font-size:11px">And</font><font style="color:blue">&gt;</font><font style="font-size:11px">&lt;/</font><font style="font-weight:bold;color:black">Where</font><font style="font-size:11px">&gt;&quot;  </font></td></tr> <tr> <td> </td></tr></tbody></table></div><br>at the start comment the oQuery.ViewFields line to be sure that there are no syntax mistakes in it.<br><br>check if this helps..</font>Wed, 30 Jul 2008 07:03:46 Z2008-07-30T07:03:46Zhttp://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/076c57b1-a780-4b86-b00b-849fa8789318#466e77d5-1622-44f0-9111-ea15de602cd3http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/076c57b1-a780-4b86-b00b-849fa8789318#466e77d5-1622-44f0-9111-ea15de602cd3Tobias Zimmergren [MVP]http://social.msdn.microsoft.com/Profile/en-US/?user=Tobias%20Zimmergren%20%5bMVP%5dSPQuery with datetime field Hi,<br><br>In order to make sure that your CAML Query works as it should, you can (and in my opinion, you should) test it with the U2U CAML Query Builder: <a href="http://www.u2u.info/SharePoint/U2U Community Tools/Forms/AllItems.aspx">http://www.u2u.info/SharePoint/U2U%20Community%20Tools/Forms/AllItems.aspx</a><br><br>What this tool does, is generate queries for your automatically and of course lets you try your existing queries.<br><br>If the query is successful in the Query Builder, it should be successfull in your code aswell.<br>And as previous posted mentioned, try commenting out the viewfields line to make sure there's no errors in there.<br><br>Regards,<br>Tobias Zimmergren<br><a href="http://www.zimmergren.net">http://www.zimmergren.net</a>Wed, 30 Jul 2008 07:10:56 Z2008-07-30T07:10:56Zhttp://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/076c57b1-a780-4b86-b00b-849fa8789318#58bc02d1-c88d-404c-9142-54238eacefc4http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/076c57b1-a780-4b86-b00b-849fa8789318#58bc02d1-c88d-404c-9142-54238eacefc4LZandmanhttp://social.msdn.microsoft.com/Profile/en-US/?user=LZandmanSPQuery with datetime field DateTime values have to be converted before you can use them in a CAML query. You can use the SPUtility.CreateISO8601DateTimeFromSystemDateTime() method for this.<br><br>Also check <a href="http://ucsharp.wordpress.com/2007/10/11/9/">this blog entry</a> if you notice the time part of your DateTime values are ignored.<br>Wed, 30 Jul 2008 10:16:10 Z2008-07-30T10:16:10Zhttp://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/076c57b1-a780-4b86-b00b-849fa8789318#381ebe77-209d-4dcf-88ed-3601c9862409http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/076c57b1-a780-4b86-b00b-849fa8789318#381ebe77-209d-4dcf-88ed-3601c9862409prabhukumardhttp://social.msdn.microsoft.com/Profile/en-US/?user=prabhukumardSPQuery with datetime field hi All,<br><br>   Thanks for your response. <br><br>after executing the following code, i am getting &quot;A field or property with the name 'Created' was not found on the selected data source&quot; error. <br><font color="#2b91af" size=2> <p>SPSite</font><font size=2> spsite = </font><font color="#0000ff" size=2>new</font> <font color="#2b91af" size=2>SPSite</font><font size=2>(</font><font color="#a31515" size=2><a href="http://localhost">http://localhost</a></font><font size=2>);</p> <p></font><font color="#2b91af" size=2>SPWeb</font><font size=2> spweb = spsite.OpenWeb();</p> <p></font><font color="#2b91af" size=2>SPList</font><font size=2> splist = spweb.Lists[</font><font color="#a31515" size=2>&quot;Shared Documents&quot;</font><font size=2>];</p> <p></font><font color="#2b91af" size=2>SPQuery</font><font size=2> oQuery = </font><font color="#0000ff" size=2>new</font> <font color="#2b91af" size=2>SPQuery</font><font size=2>();</font><font color="#008000" size=2><br></font></p><font size=2> <p></p> <p></p> <p></font><font color="#2b91af" size=2>DateTime</font><font size=2> strFromDate = </font><font color="#2b91af" size=2>Convert</font><font size=2>.ToDateTime(txtFromDate.Text);</p> <p></font><font color="#2b91af" size=2>DateTime</font><font size=2> strToDate = </font><font color="#2b91af" size=2>Convert</font><font size=2>.ToDateTime(txtToDate.Text);</p> <p></font><font color="#0000ff" size=2>string</font><font size=2> convertedTime = Microsoft.SharePoint.Utilities.</font><font color="#2b91af" size=2>SPUtility</font><font size=2>.CreateISO8601DateTimeFromSystemDateTime(strFromDate);</p> <p></font><font color="#0000ff" size=2>string</font><font size=2> convertedTime1 = Microsoft.SharePoint.Utilities.</font><font color="#2b91af" size=2>SPUtility</font><font size=2>.CreateISO8601DateTimeFromSystemDateTime(strToDate);</p> <p></font><font color="#0000ff" size=2>string</font><font size=2> Query = </font><font color="#a31515" size=2>@&quot; &lt;Where&gt;&lt;And&gt;</p> <p>&lt;Leq&gt;</p> <p>&lt;FieldRef Name='Created' /&gt;</p> <p>&lt;Value Type='DateTime'&gt;&quot;</font><font size=2> + convertedTime1 + </font><font color="#a31515" size=2>@&quot;&lt;/Value&gt;</p> <p>&lt;/Leq&gt;</p> <p>&lt;Geq&gt;</p> <p>&lt;FieldRef Name='Created' /&gt;</p> <p>&lt;Value Type='DateTime'&gt;&quot;</font><font size=2> + convertedTime + </font><font color="#a31515" size=2>@&quot;&lt;/Value&gt;</p> <p>&lt;/Geq&gt;</p> <p>&lt;/And&gt; </p> <p>&lt;/Where&gt;&quot;</font><font size=2>;</p></font><font size=2> <p>oQuery.Query = Query; </font><font size=2> <p></p></font><font color="#2b91af" size=2>SPListItemCollection</font><font size=2> items = splist.GetItems(oQuery);</p></font><font color="#008000" size=2><br></font><font size=2> <p></font><font color="#2b91af" size=2>DataTable</font><font size=2> dt = items.GetDataTable();</p> <p></font><font color="#0000ff" size=2>return</font><font size=2> dt;<br></p></font>First Time it was executing fine. <br>after that the Created column will be vanished.<br>please help me...<br><br><br><br><font color="#2b91af" size=2> <p></font><font size=2><br> </p></font>Thu, 31 Jul 2008 04:13:51 Z2008-07-31T04:13:51Zhttp://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/076c57b1-a780-4b86-b00b-849fa8789318#150c2fba-b061-4c3b-b9e8-0e6b00c05a51http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/076c57b1-a780-4b86-b00b-849fa8789318#150c2fba-b061-4c3b-b9e8-0e6b00c05a51LZandmanhttp://social.msdn.microsoft.com/Profile/en-US/?user=LZandmanSPQuery with datetime field<p>I don't see you define any ViewFields in your query. The returned SPListItems only contains fields if you explicitly ask for them.<br><br>Like this:</p> <div style="border-right:#7f9db9 1px solid;border-top:#7f9db9 1px solid;font-size:11px;overflow:auto;border-left:#7f9db9 1px solid;line-height:100%! important;border-bottom:#7f9db9 1px solid;font-family:Courier New;background-color:white"> <table style="border-top-width:0px;border-left-width:0px;margin:2px 0px;width:99%;border-bottom:#eee 0px solid;border-collapse:collapse;background-color:#fff;border-right-width:0px" cellspacing=0 cellpadding=0> <colgroup> <col style="padding-left:10px;font-size:11px;border-bottom:#f7f7f7 1px solid;font-family:Courier New;white-space:nowrap"> <tbody> <tr> <td><font style="font-size:11px">public static string BuildViewFieldsXml(params string[] fieldNames)  </font></td></tr> <tr> <td style="background-color:#f7f7f7">{  </td></tr> <tr> <td>   const string </font><font style="color:red">TEMPLATE</font><font style="font-size:11px"> = @&quot;</font><font style="color:blue">&lt;</font><font style="font-size:11px">FieldRef </font><font style="color:red">Name</font><font style="font-size:11px">=</font><font style="color:blue">'{0:S}'</font><font style="font-size:11px">/&gt;&quot;;  </font></td></tr> <tr> <td style="background-color:#f7f7f7">   StringBuilder </font><font style="color:red">sb</font><font style="font-size:11px"> = </font><font style="color:blue">new</font><font style="font-size:11px"> StringBuilder();  </font></td></tr> <tr> <td>   foreach (string fieldName in fieldNames)  </td></tr> <tr> <td style="background-color:#f7f7f7">   {  </td></tr> <tr> <td>       sb.AppendFormat(TEMPLATE, fieldName));  </td></tr> <tr> <td style="background-color:#f7f7f7">   }  </td></tr> <tr> <td>   return sb.ToString();  </td></tr> <tr> <td style="background-color:#f7f7f7">}  </td></tr> <tr> <td> </td></tr> <tr> <td style="background-color:#f7f7f7">// Example usage:  </td></tr> <tr> <td>SPQuery </font><font style="color:red">query</font><font style="font-size:11px"> = </font><font style="color:blue">new</font><font style="font-size:11px"> SPQuery();  </font></td></tr> <tr> <td style="background-color:#f7f7f7"></font><font style="color:red">query.ViewFields</font><font style="font-size:11px"> = </font><font style="color:blue">BuildViewFieldsXml</font><font style="font-size:11px">(&quot;Title&quot;, &quot;Created&quot;, &quot;SomeCustomField&quot;);  </font></td></tr> <tr> <td></font><font style="color:red">query.Query</font><font style="font-size:11px"> = ...;  // You know the drill  </font></td></tr> <tr> <td style="background-color:#f7f7f7"> </td></tr></tbody></table></div>Fri, 01 Aug 2008 07:15:52 Z2008-08-01T07:15:52Z