<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>SharePoint Fun &#187; SPQuery</title>
	<atom:link href="http://blog.qumsieh.ca/tag/spquery/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.qumsieh.ca</link>
	<description>Developer's blog related to ASP.NET, SharePoint and Telerik Web Controls</description>
	<lastBuildDate>Fri, 27 Jan 2012 19:08:36 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>SPQuery Value Type Lookup Fails on Indexed Columns</title>
		<link>http://blog.qumsieh.ca/2009/05/12/spquery-value-type-lookup-fails-on-indexed-columns/</link>
		<comments>http://blog.qumsieh.ca/2009/05/12/spquery-value-type-lookup-fails-on-indexed-columns/#comments</comments>
		<pubDate>Wed, 13 May 2009 06:32:30 +0000</pubDate>
		<dc:creator>shereen</dc:creator>
				<category><![CDATA[SharePoint 2007]]></category>
		<category><![CDATA[SPQuery]]></category>

		<guid isPermaLink="false">http://blog.qumsieh.ca/?p=436</guid>
		<description><![CDATA[Hey all, I just wanted to point out a sneaky little problem we ran into today at work. I&#8217;ll outline it briefly here, but Donabel wrote a great summary that we&#8217;ve posted on the Black Ninja blog. The problem to be aware of is related to SPQuery objects that have the LookupId set to TRUE [...]]]></description>
			<content:encoded><![CDATA[<p>Hey all, I just wanted to point out a sneaky little problem we ran into today at work. I&#8217;ll outline it briefly here, but Donabel wrote a great summary that we&#8217;ve posted on the <a href="http://blackninjasoftware.com/2009/05/12/why-should-indexing-a-sharepoint-field-break-your-caml-query/">Black Ninja blog</a>.</p>
<p>The problem to be aware of is related to <strong>SPQuery</strong> objects that have the <strong>LookupId</strong> set to <strong>TRUE</strong> and the <strong>Value Type</strong> set to <strong>Lookup</strong>. An example:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">SPQuery oQuery <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SPQuery<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
oQuery.<span style="color: #0000FF;">Query</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;&lt;Where&gt;&lt;Eq&gt;&lt;FieldRef Name='Employee' LookupId='TRUE'/&gt;&quot;</span> <span style="color: #008000;">+</span>
            <span style="color: #666666;">&quot;&lt;Value Type='Lookup'&gt;161&lt;/Value&gt;&lt;/Eq&gt;&lt;/Where&gt;&quot;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>With a <strong>NON</strong> indexed column, the above query works fine. Once you change the <strong>Employee</strong> field so it is an indexed column, the above query will fail to work. Changing <strong>Value Type</strong> from <strong>Lookup</strong> to <strong>Integer</strong> seems to make it work again. So somehow, something is changing when a column is indexed thus causing the query to behave strangely.</p>
<p>Removing the index on that column makes everything work magically again. Thanks to the following <a href="http://www.sharepointblogs.com/cwogle/archive/2008/12/15/indexed-list-columns-break-filtered-views.aspx">post</a> for affirming what we had already painfully discovered.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.qumsieh.ca/2009/05/12/spquery-value-type-lookup-fails-on-indexed-columns/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How to Write An SPQuery Using Multiple AND OR Operators</title>
		<link>http://blog.qumsieh.ca/2009/04/28/how-to-write-an-spquery-using-multiple/</link>
		<comments>http://blog.qumsieh.ca/2009/04/28/how-to-write-an-spquery-using-multiple/#comments</comments>
		<pubDate>Wed, 29 Apr 2009 00:53:26 +0000</pubDate>
		<dc:creator>shereen</dc:creator>
				<category><![CDATA[SharePoint 2007]]></category>
		<category><![CDATA[SPQuery]]></category>

		<guid isPermaLink="false">http://blog.qumsieh.ca/?p=408</guid>
		<description><![CDATA[Ok, if you&#8217;re like me, writing CAML queries can sometimes make your head hurt. Not because it&#8217;s terribly complicated, but because there&#8217;s not much useful information out there that demonstrates, with REAL examples, how these should be written. So let&#8217;s see if we can clarify this: Scenario 1 Get me all items in a list [...]]]></description>
			<content:encoded><![CDATA[<p>Ok, if you&#8217;re like me, writing CAML queries can sometimes make your head hurt. Not because it&#8217;s terribly complicated, but because there&#8217;s not much useful information out there that demonstrates, with REAL examples, how these should be written. So let&#8217;s see if we can clarify this:</p>
<h3>Scenario 1</h3>
<p>Get me all items in a list <strong>WHERE</strong> fullName equals the currently logged in user.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">SPWeb web <span style="color: #008000;">=</span> SPControl.<span style="color: #0000FF;">GetContextWeb</span><span style="color: #000000;">&#40;</span>Context<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #FF0000;">string</span> fullName <span style="color: #008000;">=</span> web.<span style="color: #0000FF;">CurrentUser</span>.<span style="color: #0000FF;">Name</span><span style="color: #008000;">;</span>
&nbsp;
SPQuery oQuery <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SPQuery<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
oQuery.<span style="color: #0000FF;">Query</span> <span style="color: #008000;">=</span> 
    <span style="color: #666666;">&quot;&lt;Where&gt;&quot;</span> <span style="color: #008000;">+</span> 
    <span style="color: #666666;">&quot;&lt;Eq&gt;&lt;FieldRef Name='FullName'/&gt;&lt;Value Type='Text'&gt;'&quot;</span> <span style="color: #008000;">+</span> fullName <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;'&lt;/Value&gt;&lt;/Eq&gt;&quot;</span> <span style="color: #008000;">+</span> 
    <span style="color: #666666;">&quot;&lt;/Where&gt;&quot;</span> <span style="color: #008000;">+</span>
    <span style="color: #666666;">&quot;&lt;OrderBy&gt;&lt;FieldRef Name='StartTime' Ascending='FALSE'&gt;&lt;/FieldRef&gt;&lt;/OrderBy&gt;&quot;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<h3>Scenario 2</h3>
<p>Get me all items in a list <strong>WHERE</strong> fullName equals the currently logged in user <strong>AND</strong> status equals &#8216;Complete&#8217;.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">SPWeb web <span style="color: #008000;">=</span> SPControl.<span style="color: #0000FF;">GetContextWeb</span><span style="color: #000000;">&#40;</span>Context<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #FF0000;">string</span> fullName <span style="color: #008000;">=</span> web.<span style="color: #0000FF;">CurrentUser</span>.<span style="color: #0000FF;">Name</span><span style="color: #008000;">;</span>
&nbsp;
SPQuery oQuery <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SPQuery<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
oQuery.<span style="color: #0000FF;">Query</span> <span style="color: #008000;">=</span> 
    <span style="color: #666666;">&quot;&lt;Where&gt;&quot;</span> <span style="color: #008000;">+</span>
    <span style="color: #666666;">&quot;&lt;And&gt;&quot;</span> <span style="color: #008000;">+</span>
        <span style="color: #666666;">&quot;&lt;Eq&gt;&lt;FieldRef Name='FullName'/&gt;&lt;Value Type='Text'&gt;'&quot;</span> <span style="color: #008000;">+</span> fullName <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;'&lt;/Value&gt;&lt;/Eq&gt;&quot;</span> <span style="color: #008000;">+</span>
        <span style="color: #666666;">&quot;&lt;Eq&gt;&lt;FieldRef Name='Status'/&gt;&lt;Value Type='Text'&gt;Complete&lt;/Value&gt;&lt;/Eq&gt;&quot;</span> <span style="color: #008000;">+</span> 
    <span style="color: #666666;">&quot;&lt;/And&gt;&quot;</span> <span style="color: #008000;">+</span>
    <span style="color: #666666;">&quot;&lt;/Where&gt;&quot;</span> <span style="color: #008000;">+</span>
    <span style="color: #666666;">&quot;&lt;OrderBy&gt;&lt;FieldRef Name='StartTime' Ascending='FALSE'&gt;&lt;/FieldRef&gt;&lt;/OrderBy&gt;&quot;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<h3>Scenario 3</h3>
<p>Get me all items in a list <strong>WHERE</strong> fullName equals the currently logged in user <strong>AND</strong> status equals &#8216;Complete&#8217; <strong>AND</strong> manager is James Lane.</p>
<p>This is where it gets a bit tricky. The following example is <strong>INCORRECT</strong> and will produce an error when run:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">SPWeb web <span style="color: #008000;">=</span> SPControl.<span style="color: #0000FF;">GetContextWeb</span><span style="color: #000000;">&#40;</span>Context<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #FF0000;">string</span> fullName <span style="color: #008000;">=</span> web.<span style="color: #0000FF;">CurrentUser</span>.<span style="color: #0000FF;">Name</span><span style="color: #008000;">;</span>
&nbsp;
SPQuery oQuery <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SPQuery<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
oQuery.<span style="color: #0000FF;">Query</span> <span style="color: #008000;">=</span> 
    <span style="color: #666666;">&quot;&lt;Where&gt;&quot;</span> <span style="color: #008000;">+</span>
    <span style="color: #666666;">&quot;&lt;And&gt;&quot;</span> <span style="color: #008000;">+</span>
        <span style="color: #666666;">&quot;&lt;Eq&gt;&lt;FieldRef Name='FullName'/&gt;&lt;Value Type='Text'&gt;'&quot;</span> <span style="color: #008000;">+</span> fullName <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;'&lt;/Value&gt;&lt;/Eq&gt;&quot;</span> <span style="color: #008000;">+</span>
        <span style="color: #666666;">&quot;&lt;Eq&gt;&lt;FieldRef Name='Status'/&gt;&lt;Value Type='Text'&gt;Complete&lt;/Value&gt;&lt;/Eq&gt;&quot;</span> <span style="color: #008000;">+</span>
        <span style="color: #666666;">&quot;&lt;Eq&gt;&lt;FieldRef Name='Manager'/&gt;&lt;Value Type='Text'&gt;James Lane&lt;/Value&gt;&lt;/Eq&gt;&quot;</span> <span style="color: #008000;">+</span>
    <span style="color: #666666;">&quot;&lt;/And&gt;&quot;</span> <span style="color: #008000;">+</span>
    <span style="color: #666666;">&quot;&lt;/Where&gt;&quot;</span> <span style="color: #008000;">+</span>
    <span style="color: #666666;">&quot;&lt;OrderBy&gt;&lt;FieldRef Name='StartTime' Ascending='FALSE'&gt;&lt;/FieldRef&gt;&lt;/OrderBy&gt;&quot;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>This is the correct way to do it:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">SPWeb web <span style="color: #008000;">=</span> SPControl.<span style="color: #0000FF;">GetContextWeb</span><span style="color: #000000;">&#40;</span>Context<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #FF0000;">string</span> fullName <span style="color: #008000;">=</span> web.<span style="color: #0000FF;">CurrentUser</span>.<span style="color: #0000FF;">Name</span><span style="color: #008000;">;</span>
&nbsp;
SPQuery oQuery <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SPQuery<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
oQuery.<span style="color: #0000FF;">Query</span> <span style="color: #008000;">=</span> 
    <span style="color: #666666;">&quot;&lt;Where&gt;&quot;</span> <span style="color: #008000;">+</span>
    <span style="color: #666666;">&quot;&lt;And&gt;&quot;</span> <span style="color: #008000;">+</span>
        <span style="color: #666666;">&quot;&lt;And&gt;&quot;</span> <span style="color: #008000;">+</span>
            <span style="color: #666666;">&quot;&lt;Eq&gt;&lt;FieldRef Name='FullName'/&gt;&lt;Value Type='Text'&gt;'&quot;</span> <span style="color: #008000;">+</span> fullName <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;'&lt;/Value&gt;&lt;/Eq&gt;&quot;</span> <span style="color: #008000;">+</span>
            <span style="color: #666666;">&quot;&lt;Eq&gt;&lt;FieldRef Name='Status'/&gt;&lt;Value Type='Text'&gt;Complete&lt;/Value&gt;&lt;/Eq&gt;&quot;</span> <span style="color: #008000;">+</span>
        <span style="color: #666666;">&quot;&lt;/And&gt;&quot;</span> <span style="color: #008000;">+</span>
        <span style="color: #666666;">&quot;&lt;Eq&gt;&lt;FieldRef Name='Manager'/&gt;&lt;Value Type='Text'&gt;James Lane&lt;/Value&gt;&lt;/Eq&gt;&quot;</span> <span style="color: #008000;">+</span>
    <span style="color: #666666;">&quot;&lt;/And&gt;&quot;</span> <span style="color: #008000;">+</span>
    <span style="color: #666666;">&quot;&lt;/Where&gt;&quot;</span> <span style="color: #008000;">+</span>
    <span style="color: #666666;">&quot;&lt;OrderBy&gt;&lt;FieldRef Name='StartTime' Ascending='FALSE'&gt;&lt;/FieldRef&gt;&lt;/OrderBy&gt;&quot;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<h3>Scenario 4</h3>
<p>Get me all items <strong>WHERE</strong> fullName equals the currently logged in user <strong>AND</strong> status equals &#8216;Complete&#8217; <strong>OR</strong> status equals &#8216;On Hold&#8217;.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">SPWeb web <span style="color: #008000;">=</span> SPControl.<span style="color: #0000FF;">GetContextWeb</span><span style="color: #000000;">&#40;</span>Context<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #FF0000;">string</span> fullName <span style="color: #008000;">=</span> web.<span style="color: #0000FF;">CurrentUser</span>.<span style="color: #0000FF;">Name</span><span style="color: #008000;">;</span>
&nbsp;
SPQuery oQuery <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SPQuery<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
oQuery.<span style="color: #0000FF;">Query</span> <span style="color: #008000;">=</span> 
    <span style="color: #666666;">&quot;&lt;Where&gt;&quot;</span> <span style="color: #008000;">+</span>
    <span style="color: #666666;">&quot;&lt;And&gt;&quot;</span> <span style="color: #008000;">+</span>
        <span style="color: #666666;">&quot;&lt;Eq&gt;&lt;FieldRef Name='FullName'/&gt;&lt;Value Type='Text'&gt;'&quot;</span> <span style="color: #008000;">+</span> fullName <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;'&lt;/Value&gt;&lt;/Eq&gt;&quot;</span> <span style="color: #008000;">+</span>
        <span style="color: #666666;">&quot;&lt;Or&gt;&quot;</span> <span style="color: #008000;">+</span>
            <span style="color: #666666;">&quot;&lt;Eq&gt;&lt;FieldRef Name='Status'/&gt;&lt;Value Type='Text'&gt;Complete&lt;/Value&gt;&lt;/Eq&gt;&quot;</span> <span style="color: #008000;">+</span>
            <span style="color: #666666;">&quot;&lt;Eq&gt;&lt;FieldRef Name='Status'/&gt;&lt;Value Type='Text'&gt;On Hold&lt;/Value&gt;&lt;/Eq&gt;&quot;</span> <span style="color: #008000;">+</span>
        <span style="color: #666666;">&quot;&lt;/Or&gt;&quot;</span> <span style="color: #008000;">+</span>
    <span style="color: #666666;">&quot;&lt;/And&gt;&quot;</span> <span style="color: #008000;">+</span>
    <span style="color: #666666;">&quot;&lt;/Where&gt;&quot;</span> <span style="color: #008000;">+</span>
    <span style="color: #666666;">&quot;&lt;OrderBy&gt;&lt;FieldRef Name='StartTime' Ascending='FALSE'&gt;&lt;/FieldRef&gt;&lt;/OrderBy&gt;&quot;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>I really do recommend if you&#8217;re doing any work with CAML queries that you download the U2U CAML Query Builder 2007. It doesn&#8217;t come without it&#8217;s issues, for example, doing a query on a list with a column of type Lookup does not always yield the results I would expect, but it&#8217;s still quite helpful. I also often find myself changing the Value Type from Lookup to Text but other than that it&#8217;s a huge resource when trying to determine if the data is actually in the list or if there is a problem with the query i&#8217;ve written.</p>
<p>Any feedback or questions, please drop me a line.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.qumsieh.ca/2009/04/28/how-to-write-an-spquery-using-multiple/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>SharePoint SPQuery Obtaining Distinct Results</title>
		<link>http://blog.qumsieh.ca/2009/01/22/sharepoint-spquery-obtaining-distinct-results/</link>
		<comments>http://blog.qumsieh.ca/2009/01/22/sharepoint-spquery-obtaining-distinct-results/#comments</comments>
		<pubDate>Fri, 23 Jan 2009 02:49:08 +0000</pubDate>
		<dc:creator>shereen</dc:creator>
				<category><![CDATA[SharePoint 2007]]></category>
		<category><![CDATA[SPQuery]]></category>

		<guid isPermaLink="false">http://blog.qumsieh.ca/?p=299</guid>
		<description><![CDATA[To my knowledge (and someone please post and correct me if I&#8217;m wrong) there doesn&#8217;t seem to be a solid way to build an SPQuery object that can return distinct values from a list. I&#8217;ve seen some clever tricks for getting around this particular problem, but nothing quite like this. If you dig through this [...]]]></description>
			<content:encoded><![CDATA[<p>To my knowledge (and someone please post and correct me if I&#8217;m wrong) there doesn&#8217;t seem to be a solid way to build an <strong>SPQuery</strong> object that can return distinct values from a list. I&#8217;ve seen some clever tricks for getting around this particular problem, but nothing quite like <a href="http://geek.hubkey.com/2008/12/get-distinct-lookup-values.html" target="_blank">this</a>.</p>
<p>If you dig through this post a little bit, you&#8217;ll find the technique to getting distinct values comes down to leveraging a <strong>DataView.ToTable()</strong> method to return only distinct results.</p>
<p>So let&#8217;s break this down a bit. A typical scenario for me is to build a data table that contains all of my data (via an SPQuery to the list), set the data source of my control to that data table and then bind.</p>
<p><em>(In my case, I work specifically with the Telerik RadGrid and the Telerik RadComboBox but the same idea applies to the standard ASP.NET controls)</em></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">DataTable groupedDataTable <span style="color: #008000;">=</span> originalDataTable.<span style="color: #0000FF;">DefaultView</span>.<span style="color: #0000FF;">ToTable</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">true</span>, <span style="color: #666666;">&quot;Title&quot;</span><span style="color: #000000;">&#41;</span>
&nbsp;
dataGrid.<span style="color: #0000FF;">DataSource</span> <span style="color: #008000;">=</span> groupedDataTable<span style="color: #008000;">;</span>
&nbsp;
dataGrid.<span style="color: #0000FF;">DataBind</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>The <strong>ToTable()</strong> method accepts <strong><span style="color: #000000;">two</span></strong> parameters as demonstrated above.</p>
<ol>
<li><strong>bool distinct</strong> simply indicates whether or not to supply distinct values</li>
<li><strong>params string[] columnNames</strong> is a string array that contains a list of the column names in the orignal data table that you&#8217;d like to be returned</li>
</ol>
<div>This worked quite nicely for me, a huge thank you to the original author of the referenced post.</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.qumsieh.ca/2009/01/22/sharepoint-spquery-obtaining-distinct-results/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Write an SPQuery to Sort Your List</title>
		<link>http://blog.qumsieh.ca/2008/11/06/how-to-write-an-spquery-to-sort-your-list/</link>
		<comments>http://blog.qumsieh.ca/2008/11/06/how-to-write-an-spquery-to-sort-your-list/#comments</comments>
		<pubDate>Fri, 07 Nov 2008 01:07:34 +0000</pubDate>
		<dc:creator>shereen</dc:creator>
				<category><![CDATA[SharePoint 2007]]></category>
		<category><![CDATA[Object Model]]></category>
		<category><![CDATA[SPQuery]]></category>

		<guid isPermaLink="false">http://blog.qumsieh.ca/?p=71</guid>
		<description><![CDATA[If you&#8217;re working with an SPListItemCollection, you might have the need to sort the data that stored in the collection. The best way I&#8217;ve found to do this is to build an SPQuery object and use that to actually query for the information. Using an object of this type makes it possible to send in [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re working with an <strong>SPListItemCollection</strong>, you might have the need to sort the data that stored in the collection. The best way I&#8217;ve found to do this is to build an <strong>SPQuery</strong> object and use that to actually query for the information. Using an object of this type makes it possible to send in whatever <strong>sort</strong> and/or <strong>orderby</strong> clause we&#8217;d like to use.</p>
<p>For example:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;OrderBy<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;FieldRef</span> <span style="color: #000066;">Name</span>=<span style="color: #ff0000;">'EventDate'</span> <span style="color: #000066;">Ascending</span>=<span style="color: #ff0000;">'FALSE'</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/FieldRef<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/OrderBy<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>The full query would look something like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">SPQuery oQuery <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SPQuery<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
oQuery.<span style="color: #0000FF;">Query</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;&lt;Where&gt;&lt;Eq&gt;&lt;FieldRef Name='AP_x0020__x002f__x0020_O'/&gt;&quot;</span> <span style="color: #008000;">+</span>
<span style="color: #666666;">&quot;&lt;Value Type='Text'&gt;&quot;</span> <span style="color: #008000;">+</span> fruitName <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;&lt;/Value&gt;&lt;/Eq&gt;&lt;/Where&gt;&quot;</span> <span style="color: #008000;">+</span>
<span style="color: #666666;">&quot;&lt;OrderBy&gt;&lt;FieldRef Name='EventDate' Ascending='FALSE'&gt;&lt;/FieldRef&gt;&lt;/OrderBy&gt;&quot;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>If you receive an error similar to:</p>
<p><font color="red">One or more field types are not installed properly. Go to the list settings page to delete these fields.</font></p>
<p>Then you&#8217;ve set the FieldRef Name incorrectly. The trick to resolving this is:</p>
<ol>
<li>Navigate to your list that the column/field is contained within</li>
<li>Click the <strong>New</strong> button as you normally would to create a new item in this list</li>
<li>Click on <strong>View</strong>, <strong>Source</strong> from the toolbar in your browser window.</li>
<li>Finally, do a find on the phrase <strong>fieldinternalname</strong> and locate the field you&#8217;re trying to query on</li>
<li>Whatever value is stored in <strong>fieldinternalname</strong> is what you&#8217;ll want to use in your query</li>
</ol>
<p>Any questions, let me know.</p>
<p><strong>UPDATE</strong>: I recently discovered another trick to this. If you want to avoid having to seek out what the internal name of a particular field is, when you first name your column, do not include any spaces or special characters. Once the field (column) has been created, go back and rename the field to include the spaces or special characters as desired. SharePoint will still retain the original field name without spaces and you can use that directly in your query without issue.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.qumsieh.ca/2008/11/06/how-to-write-an-spquery-to-sort-your-list/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

