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

share|improve this question
add comment

3 Answers 3

up vote 2 down vote accepted

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> 
share|improve this answer
add comment

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

share|improve this answer
add comment

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.

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.