Domanda

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

È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a sharepoint.stackexchange
scroll top