0

I'm developing SharePoint hosted app to calculate values from multiple lists under same site collection. I've to lookup the lists with similar names.

Ex: Root site:

/dev 

subsites:

/dev/site1 
/dev/site2 

Both site1 & site 2 have list named "timesheet".
Consider user 1 & user 2. Each user have access to both sub sites. And they updating the timesheets on both sites.

What I need is I've to calculate the a field (loggedhours) value of timesheet list from both site 1 & site 2, then add it, then store it to root site list(separate list).

Also I've to filter the values based on users only.

Ex: In site1 > timesheet - I've to calculate the items created by the current user and on Site2 > timesheet - calculate items created by current user and store it to root site.

I'm using caml query to retrieve values. But I don't know how to filter based on current user through caml query.

My code

var TimesheetList01 = web01.get_lists().getByTitle('Timesheet');

var camlQuery01 = new SP.CamlQuery();
camlQuery01.set_viewXml("<View><Query><Where></Where></Query></View>");

this.collListItem01 = TimesheetList01.getItems(camlQuery01);
currentcontext01.load(collListItem01, 'Include(Id,LoggedHours)');

currentcontext01.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
             function onQuerySucceeded(sender, args) {
             var mysum01 = 0;
              var myloggedHours01 = 0;
              var listItemInfo01 = '';
             var listItemEnumerator011 = collListItem01.getEnumerator(); 
             while (listItemEnumerator011.moveNext()) {
                var oListItem01 = listItemEnumerator011.get_current();
                  myloggedHours01 = oListItem01.get_item('LoggedHours');
                  mysum01 = mysum01 + myloggedHours01;`

Please help.

Thanks, Arun

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Arun
  • 111
  • 6

1 Answers1

0

I would update the Where in your CAML query to include (Where "Author" would be the internal name of the People field you would like to filter on):

<Where>
  <Eq>
     <FieldRef Name='Author' />
     <Value Type='Integer'>
        <UserID />
     </Value>
  </Eq>
</Where>

UserID will resolve to the UserID of the current user.

Alex
  • 325
  • 1
  • 7
  • Thanks for your update.. I've kind of different situation here. I want to retrieve the data from another list, where I've people picker field(ResourceName). That list should create by admin. So I can't use Author here.. My current code is "" But its not working.. any suggestions?? – Arun Jan 11 '16 at 06:00
  • I'm assuming the CAML you sent over is an excerpt of the full query? otherwise you're missing some nodes at the beginning. One thing I would check - is `ResourceName` the true internal name of the people column you are filtering on? Also are you receiving an error or getting no results? As you're working out your CAML query, I recommend [this tool](http://www.biwug.be/resources) for figuring out the specifics. There should be a download link on the linked page. – Alex Jan 11 '16 at 14:06
  • I suspect the data type of fields which I'm comparing with current user. Could you please mention, on UserID, what type of data will return. When I checked through CAML Query Builder, I'm getting text only. Also in my people picker field(resourcename) I'm using 'Name' type. Is there any difference between those two? There is no error, I'm not getting any data. – Arun Jan 12 '16 at 05:56