Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are 2 ways to query data (Excluding querying across an entire site collection) SPQuery and SPMetal(LINQ to SharePoint).</p> <p>In general I try to use SPMetal generated objects and only go to CAML(SPQuery) for complex queries.</p> <p>Here is 2 examples first using SPMetal objects.</p> <pre><code>ExporttestsDataContext context = new ExporttestsDataContext(SPContext.Current.Web.Url); var data = from d in context.Demo where d.Expire &lt; DateTime.Now.AddDays(5) &amp;&amp; d.Expire &gt;= DateTime.Now select d; foreach (DemoItem item in data) { //dowork } </code></pre> <p>Now SPQuery</p> <pre><code> SPList list = SPContext.Current.Web.Lists["Demo"]; SPQuery query = new SPQuery(); query.Query = "&lt;Where&gt;&lt;And&gt;&lt;Lt&gt;&lt;FieldRef Name='Expire' /&gt;&lt;Value Type='DateTime'&gt;&lt;Today OffsetDays='10' /&gt;&lt;/Value&gt;&lt;/Lt&gt;&lt;Geq&gt;&lt;FieldRef Name='Expire' /&gt;&lt;Value Type='DateTime'&gt;&lt;Today /&gt;&lt;/Value&gt;&lt;/Geq&gt;&lt;/And&gt;&lt;/Where&gt;"; SPListItemCollection items = list.GetItems(query); foreach (SPListItem item in items) { //DoWork } </code></pre> <p>For some information on CAML see <a href="http://sharepointmagazine.net/articles/writing-caml-queries-for-retrieving-list-items-from-a-sharepoint-list" rel="nofollow">http://sharepointmagazine.net/articles/writing-caml-queries-for-retrieving-list-items-from-a-sharepoint-list</a>. (I had to use OffsetDays instead of Offset to get the above working).</p> <p>Can just google SPMetal for more information on it. One tool that is nice is the CKS Development tools as it can generate this class(es) with just a few clicks in visual studio.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload