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 a CAML query which returns lists by a lookup field.

here is the query

<Query>                    
         <Where>
           <Eq>
                <FieldRef Name='Ticket' LookupId = 'TRUE' />
                <Value Type='Lookup'>75</Value>
           </Eq>
           </Where> </Query>

According to the data in my list this query should not return anything, but it returns all the rows. in a couple of articles, removing Query tag suggested as fix, but it did not fix my issue.

is there any way to find why this query does not work? I am using Client Object Model

Update: Here is the code I use to query items,

SP.ListItemCollection noteItems = null;
                List<TicketNote>  notes = new List<TicketNote>();
                using (SP.ClientContext clientContext = GetClientContext())
                {
                    SP.List oList = clientContext.Web.Lists.GetByTitle("Note");
                    SP.CamlQuery camlQuery = new SP.CamlQuery();


                    camlQuery.ViewXml = @"
                    <Query>                    
                        <Where>
                           <Eq>
                               <FieldRef Name='Ticket' LookupId = 'True' />
                               <Value Type='Lookup'>75</Value>
                            </Eq>
                        </Where></Query>";

                    noteItems = oList.GetItems(camlQuery);
                    clientContext.Load(noteItems);

                    clientContext.ExecuteQuery();

                  }

Thanks

share|improve this question
    
Are the spaces around = in FieldRef also in your code? That's not valid xml! –  Per Jakobsen Apr 24 '12 at 16:55
    
I had the same problem and the &lt;View>&lt;Query>&lt;Where>...&lt;/Where>&lt;/Query>&lt;/View> worked Both &lt;Query>&lt;Where>...&lt;/Where>&lt;/Query> and &lt;Where>...&lt;/Where> failed. –  user9695 Jul 26 '12 at 15:35
add comment

4 Answers 4

up vote 3 down vote accepted

Actually you need to wrap the QUERY tag into a VIEW. If you have fields which could be NULL it is wise to add <View><ViewFields><FieldRef Name='Title'/><ProjectProperty Name='Title' Nullable='TRUE'/></ViewFields>

share|improve this answer
    
nope did not really worked.. –  AnarchistGeek Apr 24 '12 at 16:29
1  
wrapping the query with <View> tag sorted. –  AnarchistGeek Apr 24 '12 at 16:58
add comment

Try this one. This is tried and tested..

context.Load(context.Web);
            List oList = context.Web.Lists.GetByTitle("test");
            context.Load(oList);

            string ID = "75";
            CamlQuery query = new CamlQuery()
            {
                ViewXml = @"<View>
                            <Query>
                               <Where>
                                  <Eq>
                                    <FieldRef Name='Ticket' LookupId='TRUE'  />
                                     <Value Type='Integer'>" + ID + @"</Value>
                                  </Eq>
                               </Where>
                            </Query></View>"
            };

            Item = oList.GetItems(query);
            context.Load(Item);
            context.ExecuteQueryAsync(sucess, failed);
share|improve this answer
add comment

Try to use Integer as value type:

<Value Type='Integer'>75</Value>
share|improve this answer
    
Nope nothing changed... –  AnarchistGeek Apr 24 '12 at 16:13
    
updated the code. –  AnarchistGeek Apr 24 '12 at 16:15
1  
Actually you need to wrap the QUERY tag into a VIEW. If you have fields which could be NULL it is wise to add <View><ViewFields><FieldRef Name='Title'/><ProjectProperty Name='Title' Nullable='TRUE'/></ViewFields>....etc. –  C. Marius - MVP Apr 24 '12 at 16:31
    
@C.Marius could you update your answer with the comment you made as it fixed my issue. –  AnarchistGeek Apr 24 '12 at 16:57
add comment

Use :

<Where>            
      <Eq>                 
          <FieldRef Name='Ticket' LookupId = 'TRUE' />
          <Value Type='Lookup'>75</Value>
       </Eq>
</Where> 

Instead of :

<Query>                              
   <Where>            
      <Eq>                 
          <FieldRef Name='Ticket' LookupId = 'TRUE' />
          <Value Type='Lookup'>75</Value>
       </Eq>
   </Where> 
</Query>
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.