2

I'm trying to find a way to identify which user has added more items on a list. Is there any way through REST API to identify the user? I'm using SharePoint Online.

  • One possibility would be to create an OOTB view for your list which groups the items by Created By. Additionally you can add the totals to your view, so the view does the counting for you. – moe Jul 18 '17 at 17:20
  • Thanks @moe. I need to do using jquery REST API or CSOM. Let me know if any other option please. – Srini K Jul 18 '17 at 17:55
1

I don't know the code off the top of my head but here's an idea, little big long winded but it should work in theory.

  1. Issue a query to get all the values of 'Created By' (and only the values of that field, nothing else)
  2. Loop through the values returned and remove any non-unique values (i.e. add a name to a new collection of names only on the first time it is seen)
  3. With the new collection (which contains all users who appear in Created By with no duplicates), for each user, issue a query to select the count of items where Created By is equal to that user and pick the highest one.
  • Thank you, I have implemented in same way... Cheers... – Srini K Jul 19 '17 at 19:22
  • Good stuff. Can you mark my response as the correct answer if so? Just so the Q can be closed and I get those delicious rep points. :) – Thomas Gass Jul 20 '17 at 8:02
4
  • Add a Column "Created by"
  • Add Totals for created by column for this -> Go to Modify View -> go to totals -> Change option none to count for Created by column -> Save by clicking ok
  • if you click on top created by column you can filter by name and you can know the count by selecting their names.

you can get to know the count by seeing this picture

  • Thank you for OOB option. However I need to do using jQuery REST API or CSOM. – Srini K Jul 18 '17 at 17:54
  • Do comment your Answer after you figure it out – AIM4DREAM Jul 18 '17 at 18:42
  • 1
    Navi9, I have added my implementation above. Cheers. – Srini K Sep 14 '17 at 20:50
2

Grouping or aggregate functions are not support with REST API. You will need to get the Authors from the rest API endpoint something like this:

_api/Web/Lists/getByTitle('TargetList')/items?$select=Title, Author/Title, Author/Id&$expand=Author

Write Jscript/Jquery code to group them by author ID. Have a look at this

2

The following solution i didn't try but based on my knowledge i am just giving a suggestion.

Use CAML + REST Api to get max count.

In CAML query we can you Group By filter. After getting items, we can easily get max count from results using JQuery/Javascript.

Hope this helps you.

  • Thank you. Finally I did the same way to finish off. There is no way to get the aggregate count either in REST/CSOM. – Srini K Jul 19 '17 at 19:20
0

Finally, I used below script to iterate items and get top contributor. One caveat is I used to retrieve top 5k items at a time.

for (var i = 0; i < itemcount; i++) {
                oTopContributor = { "id": itemsresult[i].Id, "authorID":  itemsresult[i].AuthorId };
                objJSON.push(oTopContributor);
            }
            //Below piece of code will group by authorID and get the count
            var occurences = objJSON.reduce(function (r, row) {
            r[row.authorID] = ++r[row.authorID] || 1;
                return r;
            }, {});
            var result = Object.keys(occurences).map(function (key) {
                return { key: key, value: occurences[key] };
            });
            //End of grouping and get count
            var prop='value';
            result.sort(function(a, b) {
                    return (b[prop] > a[prop]) ? 1 : ((b[prop] < a[prop]) ? -1 : 0);
            });
            objJSON = result;
            //Get top contributor details
            if(objJSON.length >1){
               var userid1 = objJSON[1]["key"];// top contributor userid
               var noOfItems1 = objJSON[1]["value"];// no. of items contributed
            }

Hope it will be useful for someone. Happy programming :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.