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

2
  • Are the spaces around = in FieldRef also in your code? That's not valid xml! Commented Apr 24, 2012 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
    Commented Jul 26, 2012 at 15:35

4 Answers 4

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>

2
  • nope did not really worked.. Commented Apr 24, 2012 at 16:29
  • 1
    wrapping the query with <View> tag sorted. Commented Apr 24, 2012 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>
4
  • Nope nothing changed... Commented Apr 24, 2012 at 16:13
  • updated the code. Commented Apr 24, 2012 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. Commented Apr 24, 2012 at 16:31
  • @C.Marius could you update your answer with the comment you made as it fixed my issue. Commented Apr 24, 2012 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 and acknowledge you have read our privacy policy.

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