Sharepoint online - search/rest api -filter results in URL

Iron Contributor

I've got this piece of code to query a certain content type:

	var results = [];
  		$.ajax({
    		url: "https://blabla.sharepoint.com/_api/search/query?querytext='ContentTypeId:0x0100AEC702D446F8F04696C2B7573837C451*'&trimduplicates=false&rowlimit=500&selectproperties='FullName%2cWorkHours%2cWorkDate'",
    		method: "GET",
    		headers: {"accept": "application/json;odata=verbose"},
			success: function(xData, request){
            results = xData.d.query.PrimaryQueryResult.RelevantResults.Table.Rows.results;

This is working fine. But, as you can see it gives me all records (which can be a lot). In my current solution I build an If statement to filter "FullName" on the Current User. It works but performance wise is doe not seem the best solution. So I would like the initial query to give me only the records of the current user.

 

 

I tried several queries like this:

 

url: "https://blabla.sharepoint.com/_api/search/query?querytext='ContentTypeId:0x0100AEC702D446F8F04696C2B7573837C451*'&trimduplicates=false&rowlimit=500&selectproperties='FullName%2cWorkHours%2cWorkDate'?$filter FullName eq CurrentUser",

In all kind of different ways. But no luck so far.

 

 

When I have this in place I also would like to aggegate the hours per day.  But that will be phase two ;)

 

Can someone help me out?

4 Replies

I figured it out:

 

    		url: "https://bla.sharepoint.com/_api/search/query?querytext='ContentTypeId:0x0100AEC702D446F8F04696C2B7573837C451*'&selectproperties='FullName%2cWorkHours'&refinementfilters='FullName:equals(%22"+ VarName +"%22)'",

Now I would like to sort and sum my data based on the date.

A user enters his working hours on different projects a day.

 

I my query I sort on the date. This returns something like "Wed Mar 1 15:14:44 UTC +100 2017"

All I need is to sum all hours with (for example) date "Wed Mar 1". I tried to do some loops again but that doen not look very good, performance wise (again).

 

Did someone ever do something like this before?

you should have a look at lodash. it has methods to do these kinds of things,

@Russell Gove I'll look into that when I have some time left. For now I ended up doing some old fashion javascript:

			  	var oldDate = formatDate(results[0].Cells.results[6].Value);
			  	
			  	for(var i=0;i<results.length;i++){
					
					var newDate = formatDate(results[i].Cells.results[6].Value);
					if (oldDate == newDate){
						TotalMinutes = +TotalMinutes + +results[i].Cells.results[4].Value;
					} else {
						var d=new Date(oldDate);
						[do something]
						oldDate = formatDate(results[i].Cells.results[6].Value);
						TotalMinutes = +results[i].Cells.results[4].Value;
					}	
				}       

function formatDate(d)
 {
  date = new Date(d)
  var dd = date.getDate(); 
  var mm = date.getMonth()+1;
  var yyyy = date.getFullYear(); 
  if(dd<10){dd='0'+dd} 
  if(mm<10){mm='0'+mm};
  return d = mm+'/'+dd+'/'+yyyy
}