Arun
Arun

Reputation: 111

Get value from multiple lists

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

Upvotes: 0

Views: 802

Answers (1)

Alex
Alex

Reputation: 345

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.

Upvotes: 0

Related Questions