Tuesday, August 31, 2010

SPQuery Returns All of the Items

During a development project, calling SPQuery returned all items from list! Looks like it just ignored the filters we applied.

SPList list = new SPSite("http://MySharePointSite").OpenWeb().Lists[listName];

SPQuery query = new SPQuery();

query.Query = "<Query><Where><Eq><FieldRef Name="Title" /><Value Type="Text">Test</Value></Eq></Where></Query>";

SPListItemCollection items = list.GetItems(query);


Fix is: This above code returns all items in the list, but if you remove the <Query> tag all works fine:

SPList list = new
SPSite("http://MySharePointSite").OpenWeb().Lists[listName];


SPQuery query = new SPQuery();

query.Query = "<Query><Where><Eq><FieldRef Name="Title" /><Value Type="Text">Test</Value></Eq></Where></Query>"; 

SPListItemCollection items = list.GetItems(query);

Remember, Query XML is case sensitive! So <Where> is not equal to <where>



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

1 comment :

  1. the code is working, but it is not retrieving all items because of the value .

    ReplyDelete

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...