locked
SPQuery filter returns all list items RRS feed

  • Question

  • Hi guys,

    I have an issue with a very simple CAML and blown out of its filtering. The query is

    curQry.Query = "<Query><Where><Eq><FieldRef Name='Title' /><Value Type='Text'>letter</Value></Eq></Where></Query>";

    and on that source list library I have the following items:

    Title Value

    Memo  3

    Letters  66

    Drawing 12

    Note the item entry is with s, I am doing that on purpose to validated the return items of query. Its so confusing when the query return a match with the item Letters  but when the query execute in CALM builder it doesnt return anything; which is logically correct. Letter and Letters are definitely not equal.

    Here is another catch, when I run the  query against the source library list

    curQry.Query = "<Query><Where><Eq><FieldRef Name='Title' /><Value Type='Text'>xxxxx</Value></Eq></Where></Query>";

    it returns all list item entry. I do code break to check the que item.

    curQry.RowLimit = 100;
    
    foreach (SPListItem curItem in curItems){
                  Console.WriteLine(curItem["Title"].ToString());
     }
                  

    Any ideas. Thanks in advance.


    Cheers, R2C3 ----------------------------------- starting sharepoint :)

    Sunday, April 29, 2012 6:01 AM

Answers

  • Hi R2C3,

    As per the MSDN documentation, the spQuery.Query property corresponds to the inner XML of the Query element in CAML, excluding the opening and closing <Query></Query> tags.

    So in your case it should be:

    curQry.Query = "<Where><Eq><FieldRef Name='Title' /><Value Type='Text'>letter</Value></Eq></Where>";

    You may also like to have a look at the spQuery.ViewXml property which completly describes query. It can contain Query, ViewFields, RowLimit elements and many more. 

    Just have a brief look at this SPQuery.ViewXml Property using ViewXml property of the SPQuery object.


    • Edited by Devang Bhavsar Sunday, April 29, 2012 7:07 AM
    • Proposed as answer by SanRang Monday, April 30, 2012 12:52 AM
    • Marked as answer by Lhan Han Tuesday, May 1, 2012 1:49 AM
    Sunday, April 29, 2012 6:52 AM

All replies

  • Remove <Query> and </Query>

    백상하 - Stop hacking my blog http://blog.naver.com/agilepoint
    쉐어포인트 배움터 http://sharepointkorea.com


    • Edited by Sangha Baek MVP Sunday, April 29, 2012 6:04 AM
    • Proposed as answer by SanRang Monday, April 30, 2012 12:52 AM
    Sunday, April 29, 2012 6:03 AM
  • Hi Sangha Baek,

    It works like a charm! 


    Cheers, R2C3 ----------------------------------- starting sharepoint :)

    Sunday, April 29, 2012 6:40 AM
  • Hi R2C3,

    As per the MSDN documentation, the spQuery.Query property corresponds to the inner XML of the Query element in CAML, excluding the opening and closing <Query></Query> tags.

    So in your case it should be:

    curQry.Query = "<Where><Eq><FieldRef Name='Title' /><Value Type='Text'>letter</Value></Eq></Where>";

    You may also like to have a look at the spQuery.ViewXml property which completly describes query. It can contain Query, ViewFields, RowLimit elements and many more. 

    Just have a brief look at this SPQuery.ViewXml Property using ViewXml property of the SPQuery object.


    • Edited by Devang Bhavsar Sunday, April 29, 2012 7:07 AM
    • Proposed as answer by SanRang Monday, April 30, 2012 12:52 AM
    • Marked as answer by Lhan Han Tuesday, May 1, 2012 1:49 AM
    Sunday, April 29, 2012 6:52 AM
  • Hi Devang Bhavsar,

    Thanks for the additional info.


    Cheers, R2C3 ----------------------------------- starting sharepoint :)

    Sunday, April 29, 2012 8:00 AM
  • Except if you're using the web services (at least in SP 2007) GetListItems method, you do have to wrap the query parameter in <query></query> tags. Very confusing when going back and forth between the two in development.
    Monday, June 4, 2018 10:04 PM