0

i am using a caml query to check if a specific value exist in a sharepoint list. but it ends up returnning all the values instead of one.

          while (reader.Read())
                {
                 //   MessageBox.Show(reader[5].ToString());
                    CamlQuery camlQuery = new CamlQuery();
                    camlQuery.ViewXml =
                        @"<Query>
<Where>
  <Eq>
    <FieldRef Name='Email_x0020_Address' />
    <Value Type='Text'>myname@domain.com</Value> </Eq></Where></Query>";
                    ListItemCollection listItems = myList.GetItems(camlQuery);
                    ctx.Load(
                         listItems
                         );
                    ctx.ExecuteQuery();

                    if (listItems.Count != 0)
                    {
                        MessageBox.Show("Item Exists");
                        foreach (ListItem listItem in listItems.ToList())
                        {
                            MessageBox.Show(listItem["Email_x0020_Address"].ToString());
                        }
                    }
                    else
                    {
                        MessageBox.Show("Item Does not exists");
                    }
1

You are missing View tag.

Please try to put your query inside View tag. It will retrieve filtered data only.

<View>
    <Query>
        <Where>
            ...
            ...
            ...
        </Where>
    </Query>
</View>
  • even with adding view tag its the same – Rinu Jun 28 '17 at 10:20
-1

Removing the Query keyword could work.

camlQuery.ViewXml = @"<Where><Eq><FieldRef Name='Email_x0020_Address' /<Value Type='Text'>myname@domain.com</Value></Eq></Where>";

Alternatively, you can try to first append all the items in the list to a List of ListItem and then do your query by using LINQ. It was suggested to me by someone in here and I find it easier, more structured and never fails in large data sets (unlike CAML Query which does not work with over 5000 items).

  • not working with this <Where> <Eq> <FieldRef Name='Email_x0020_Address' /> <Value Type='Text'>myemail@mydomain.com</Value> </Eq></Where>. but when i ran it directly in caml query builder it returns only one record. from visual studio its 1017 the total number of records – Rinu Jun 28 '17 at 11:56

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.