Tuesday, October 9, 2007

SPQuery returns all items

If you execute an SPQuery through the SPList.GetItems(SPQuery) method and get all the items in the list, check the syntax in the Query property. The U2U CAML Query adds the tag <Query> to the query and it is not necessary in the Query property of SPQuery. For example:

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);

This 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);

Related Links:
http://joeshepherd.spaces.live.com/Blog/cns!9AE2097A4A610B63!123.entry

10 comentarios:

Kuper said...

Hi,
Delete from query text "Query" tag and recompile it.

Anonymous said...

Thanks for the help!

Robi said...

Thanks alot :), at first i really didn't understand why the query was working fine in U2U but not in actual code.

Matt said...

Beautiful......

KK said...

Thanks a lot. In my case i had to remove 'Query' and 'View' tags to make it work.

Anonymous said...

Thanks, this really helped

Surya said...

Hi,

I have one small doubt, how can i show xml tags while posting. for example u have shown xml tags in ur poting query.Query = some xml tags

Thanks,
Surya.

Jani said...

Tnx, really helped

Angelo said...

thanks. this saved me.

AnDrE said...

Thanks a lot!! I was going crazy with this issue!! Thanks Thanks Thanks :)

 
Online Visitors