2

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

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

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>

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

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);
1

Try to use Integer as value type:

<Value Type='Integer'>75</Value>
  • 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. – Marius Constantinescu - 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
0

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>

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.