1

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
  • 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
0

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.

1

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>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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