5

I have this CAML below and i'm tring to do something like this. Where username ='username' and id = 'id'

<Query>
     <Where>
         <And>
              <Eq>
                  <FieldRef Name='username'/>
                  <Value Type='Text'>" + username + "</Value>
              </Eq>
              <And>
                  <Eq>
                       <FieldRef Name='Title'/>
                       <Value Type='Text'>" + ID + "</Value>
                  </Eq>
              </And>
         </And>
     </Where>
 </Query>

But it seems somehting is wrong with it. Any ideas what i'm doing wrong and thanks in advance

3 Answers 3

5

You can query SPUserField as SPLookupField

<Query> 
     <Where> 
         <And> 
              <Eq> 
                  <FieldRef Name='UserField'/> 
                  <Value Type='Lookup'>" + username + "</Value> 
              </Eq> 
              <Eq> 
                   <FieldRef Name='UserField' LookupId='True' /> 
                   <Value Type='Lookup'>" + ID + "</Value> 
              </Eq>
         </And> 
     </Where> 
 </Query> 
7

Your syntax is wrong, there's to many "Ands". I also prefer to lookup ID like IAfanasov says.

<Query> 
     <Where> 
         <And> 
              <Eq> 
                  <FieldRef Name='UserField'/> 
                  <Value Type='Lookup'>" + username + "</Value> 
              </Eq>
              <Eq> 
                   <FieldRef Name='UserField' LookupId='True' /> 
                   <Value Type='Lookup'>" + ID + "</Value> 
              </Eq>
         </And> 
     </Where> 
 </Query> 

This is a nice starter/reference: http://sharepointmagazine.net/articles/writing-caml-queries-for-retrieving-list-items-from-a-sharepoint-list

0

Remove the <Query> </Query> part from the text. Your CAML Query should begin with <Where> and end with </Where>

Try using the U2U Caml Query builder to test your CAML queries. OR simply use Camlex.net which would convert your CAML to .Net. Converting to .Net helps you analyzing expression errors in compile time itself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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