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

30 comments:

Anonymous 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.

Unknown 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 Pulipati 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.

Unknown said...

Tnx, really helped

Unknown said...

thanks. this saved me.

Filhos, Laços e Abraços said...

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

Gustavo Bonansea said...

Dear friend, I had the same problem and you really help me, thanks.
When you come back to Argentina ? People in San Luis are awaiting us :)

Cheers.

Aniki said...
This comment has been removed by the author.
Aniki said...

Thanks a lot!! I find this solution for 3 days,when I went to give up, I find your post, Thank you Very much,again!

Aniki said...

Thanks a lot!! I find this solution for 3 days,when I went to give up, I find your post, Thank you Very much,again!

Anonymous said...

Nice !!!!!!!!

SpQuery and CAML in Sharepoint, it is simple.

Try this too,
SpQuery and CAML in

Sharepoint

Unknown said...

Hi,
I am facing a new issue with getitems method. I need all items that are present only in the immediate level of the folder and not from sub folders. As its known that by default spquery returns all the items from the immediate level if we are not spcifying the viewattributes value.
I did in the same but without specifying the viewattributes value by default its displaying all the items including subfolder items.
Can you please suggest me how to get my problem solved?

Àlex said...

Mithun,
You can specify the folder using the QueryOptions tag in the CAML query:
<QueryOptions>
<Folder>Documents/Test</Folder>
</QueryOptions>

Where 'Documents' is the name of the document library and 'Test' the folder name.

I recommend you to use the U2U CAML Query Builder that helps you to construct the CAML query.

Chuck Danner said...

Thanks! You're Awesome!

Anand said...

Hi,
I'm facing the same problem.

I don't have tag in my CAML query & still it's not working.

Though it's working in U2U Query builder.

Thanks in advance,
Anand

Unknown said...

Brilliant! have been struggling on this for ages now!

Thank you!

EaglesEye said...

Thanks!!!

Anonymous said...

Tom: Hi.. i have an problem with the SPList.GetItems(view) method.
My View has 3 pages with 300 items. But when ich use the SPList.GetItems(view) method, i get just 100 items as result. That is the the result of the first 100 elements on my first page. But where are the elements from the second and third page? Do yoh have any suggestions? Thanks. Tom

Àlex said...

Tom,
Probably, you need to set the row limit parameter.

Mota Landh said...

query.ViewAttributes = "Scope='RecursiveAll'";

or

query.ViewAttributes = "Scope=\"Recursive\"";

Make sure to set this up else you wont get the full list.

Pablo Bertrão said...

Thanks. I was stuck 'till i found this solution. :D

Anonymous said...

You save my life!

Anonymous said...

Thank you Thank you Thank you!!! Microsoft forgot to tell us this.

Avinash Maheshwari said...

nice.. Once you have CAML query
You can get list items from sharepoint using SPQuery
Check it out this also :

How to get list items using caml and SPQuery

Paolo said...

Thank you very much!

erectile dysfunction pills said...

Great article, totally what I was looking for.

 
Online Visitors