Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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= "
    <Where>
           <And>
                   <Contains>
                         <FieldRef Name='List'/>
                         <Value Type='String'>"+myID+"</Value>
                  </Contains>
                <Eq>
                        <FieldRef Name='URL'/>
                        <Value Type='URL'>"+myUrl+"</Value>
                </Eq>
          </And>
     </Where>";

      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
1  
Have you tried <Value Type="Text"> instead of with String? –  eirikb Jan 15 '13 at 6:33
add comment

2 Answers 2

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
add comment

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:

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

Your Answer

 
discard

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.