1

I'm using SharePoint 2010 and SPServices to retrieve the elements of a list, but no all items,

My list have these fields:

  • Year: calculated column
  • Month: Choice column
  • Company: Line of text
  • Department: Choice Column
  • Type: Choice Column
  • Cost: Line of text

I want to filter the list by Year, Month, Company, Department, and Type so this is the code:

$().SPServices({ operation: "GetListItems", async: false, listName: "{list guid}", viewName: "{view guid}", CAMLViewFields: "<ViewFields><FieldRef Name='Title'/><FieldRef Name='Cost' /></ViewFields>", CAMLQuery:"<Query><Where><And><Eq><FieldRef Name='Year'/><Value Type='Integer'>2016</Value></Eq><Eq><FieldRef Name='Month' /><Value Type='Integer'>11</Value></Eq></And><And><Eq><FieldRef Name='Company' /><Value Type='Text'>CompanyName</Value></Eq><Eq><FieldRef Name='Department' /><Value Type='Text'>IT</Value></Eq></And><Eq><FieldRef Name='Type' /><Value Type='Text'>two</Value></Eq></Where></Query>", completefunc: function (xData, Status) { $(xData.responseXML).SPFilterNode("z:row").each(function() { var liHtml =$(this).attr("ows_Title"); var Cost =$(this).attr("ows_Cost"); }); } }); 

My problem is that a can't retrieve anything and the next step with all the items retrieved o want to sum all there Cost values If you guys can help me I really appreciate

1 Answer 1

1

You need to include the columns in view fields which are being used in Filters(CAMLQuery).

To sum up the cost, once you receive you can loop through item and use simple mathematical calculations. Below is modified code which may get you started..

$().SPServices({ operation: "GetListItems", async: false, listName: "{list guid}", viewName: "{view guid}", CAMLViewFields: "<ViewFields><FieldRef Name='Title'/><FieldRef Name='Cost' /><FieldRef Name='Year'/><FieldRef Name='Month'/><FieldRef Name='Company'/><FieldRef Name='Department'/><FieldRef Name='Type'/></ViewFields>", CAMLQuery:"<Query><Where><And><Eq><FieldRef Name='Year'/><Value Type='Integer'>2016</Value></Eq><Eq><FieldRef Name='Month' /><Value Type='Integer'>11</Value></Eq></And><And><Eq><FieldRef Name='Company' /><Value Type='Text'>CompanyName</Value></Eq><Eq><FieldRef Name='Department' /><Value Type='Text'>IT</Value></Eq></And><Eq><FieldRef Name='Type' /><Value Type='Text'>two</Value></Eq></Where></Query>", completefunc: function (xData, Status) { var Cost =0; $(xData.responseXML).SPFilterNode("z:row").each(function() { var liHtml =$(this).attr("ows_Title"); Cost += $(this).attr("ows_Cost"); }); } }); 

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.