SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to check a list for related values but for some reason every item in the list is returned each time. I have seen this issue before and for me its always meant a bad CAML string but I have not been able to figgure out what is wrong.

Table contains two extra fields:

 FieldName | Type
    'List'        | GUID
    'URL'         | URL

Current Query:

 SPWeb web = SPContext.Current.Web;
 String myID=web.Lists["myList"].ID.ToString();
 String myUrl = web.Url;

 SPQuery MyQuery = new SPQuery();
 MyQuery.Query= "
                         <FieldRef Name='List'/>
                         <Value Type='String'>"+myID+"</Value>
                        <FieldRef Name='URL'/>
                        <Value Type='URL'>"+myUrl+"</Value>

      SPListItemCollection results = web.Lists["sourceList"].GetItems(MyQuery);

I have also tried with in place of and Text in place of String.

  • I do know the (string)list.ID. is same format as my GUID Field which is why matching by string should work
share|improve this question
returning all items on GetItems() is a common side-effect of the query being invalid. I'd check it in CAML Query Builder or similar and see if it does what you expect. – TZHX Jan 14 '13 at 22:47
Have you tried <Value Type="Text"> instead of with String? – eirikb Jan 15 '13 at 6:33
up vote 0 down vote accepted

Your query seems fine to me, I would suggest using a nice tool that will generate you a caml query so you can compare the results. It's free and called CamlDesigner.

Try it.
Good luck.

share|improve this answer

If your List field is of SPFieldType.Guid, then you should rather use strict comparison (Eq). Also I set Type attribute of Value Tag to Guid, but It is not nessesary:

  <FieldRef Name="List" />
  <Value Type="Guid">+myId+</Value>
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.