1

I am experimenting with some Rest API code. My knowledge of javascript and jquery is basic but I know enough to usually get what I need. Usually through trial and error. Here is what I am attempting to do:

  1. Using "/_api/web/sitegroups/getByName('Intranet Team')/users" to return the ID of all members of that permission group.
  2. Then run those IDs through "/teams/Sales/_api/web/lists/getbytitle('User Information List')/items(" + items[i].Id + ")" to get the Name, Office and other details for each member of the permission group.
  3. Lastly print the details of the User Information List into divs by Office location.

The problem I have is on step 3. I am doing something wrong with my conditional and it is returning all users regardless of the location. The following is my sample code:

$.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/sitegroups/getByName('Intranet Team')/users",
type: "GET",
headers: {"accept": "application/json;odata=verbose"},
success: function (data){
    var items = data.d.results;
    else {
        for (var i = 0; i < items.length; i++) {        
                $.ajax({
                    url: "/teams/Sales/_api/web/lists/getbytitle('User Information List')/items(" + items[i].Id + ")",
                    method: "GET",
                    headers: { "Accept": "application/json; odata=verbose" },
                    success: function (data) {
                            var userOffice = data.d.Office;
                            console.log(userOffice);
                            if (userOffice = 'North York') {
                                $('#NorthYork').append('<h3 class="person">'+data.d.Title+' - '+data.d.Office+'</h3>');
                            }
                    },
                    error: function (data) {
                        alert(JSON.stringify(data));
                    }
                });
        }
    }}

Any help would be greatly appreciated. Martin

1 Answer 1

1

You're missing the double/triple equals on your conditional.

if (userOffice === 'North York')

Make sure you test this code using the different security contexts you expect to be accessing it. Not all users are going to be able to make these queries.

2
  • Hi Derek. Thanks for replying to my question. I had previously tried with double and triple equal. Neither returned any values. Using the single does return values but not the ones I expected. In testing the code I made two security changes. 1 change the permission group settings to allow all users to view the group membership. 2 changes the permissions of the User Information List to allow visitors read only access to it.
    – Martin
    Commented Nov 24, 2018 at 22:17
  • If the triple equals never returns true then you obviously don't have any instances of userOffice that equal 'North York'. Set a breakpoint on the if and run the comparison in the console. The issue should jump at you from there. Commented Nov 24, 2018 at 22:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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