0

I am executing this query which returns results...however it is returning ALL results, not the one specified in my where clause. How should I re-write this so that ONLY the ID in the where clause is returned?

string server = "blahblahblah";
string msn = "14318";
var ctx = new ClientContext(server);
ver web = ctx.Web;
List list = web.Lists.GetByTitle("Attendance");
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = "<ViewFields>
<FieldRef Name='recID'/>
<FieldRef Name='MasterID'/>
<FieldRef Name='totalsalevalue'/>
</ViewFields><where><Eq><FieldRef Name='EmployeeID' /><Value Type='Text'>"+msn+"</Value></Eq><Where>";
var listItemCollection = list.GetItems(CamlQuery.CreateAllItemsQuery());

ctx.Load(listItemCollection, eachItem => eachItem.Include(
                                          item => item,
                                          item => item["recID"],
                                          item => item["totalsalevalue"],
                                          item => item["MasterID"]));
ctx.ExecuteQuery();

foreach (ListItem li in listItemCollection)
{
    Console.WriteLine(li["recID"]);
    Console.WriteLine(li["MasterID"]);
    Console.WriteLine(li["totalsalevalue"]);
}

3 Answers 3

2

I don't have access to SharePoint to test this right now, but try this:

<View>
  <ViewFields>
    <FieldRef Name='recID'/>
    <FieldRef Name='MasterID'/>
    <FieldRef Name='totalsalevalue'/>
  </ViewFields>
  <Query>
    <Where>
      <Eq>
        <FieldRef Name='EmployeeID' />
        <Value Type='Text'>"+msn+"</Value>
      </Eq>
    </Where>
  </Query>
</View>

And as a test, try just the Where portion to see if it works:

    <Where>
      <Eq>
        <FieldRef Name='EmployeeID' />
        <Value Type='Text'>"+msn+"</Value>
      </Eq>
    </Where>
1
  • Adding in the <View> and <Query> tags solved. Thank you! Commented Mar 16, 2018 at 17:23
5

You're not passing your camlQuery object to the GetItems method. You should change this line:

var listItemCollection = list.GetItems(CamlQuery.CreateAllItemsQuery());

to this:

var listItemCollection = list.GetItems(camlQuery);
3
  • Even after changing to your recommend ALL data is returned Commented Mar 16, 2018 at 14:26
  • You should check two more things: 1) Is the EmployeeID column really the text column? 2) How do you create the ViewXml string (camlQuery.ViewXml = ...) because the way it is written in the original question doesn't compile (you should escape double quotes in the string or use single quotes). Commented Mar 16, 2018 at 14:43
  • See edit -> and yes EmployeeID is really the text column Commented Mar 16, 2018 at 14:51
1

Your XML is not valid:

<where><Eq><FieldRef Name='EmployeeID' /><Value Type='Text'>"+msn+"</Value></Eq><Where>

Probably should be:

<Where><Eq><FieldRef Name='EmployeeID' /><Value Type='Text'>"+msn+"</Value></Eq></Where>
1
  • Even after making the change from where to Where all data is still returned Commented Mar 16, 2018 at 14:26

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.