Search code examples
sharepointmicrosoft-graph-apispfx

How to iterate through Graph data from an SPFx web part?


Sorry for the dumb question!

In my code below I get all the members of a M365 group and they display in the console.log.

However I don't know how to iterate through the JSON that is returned.

Thanks P

    const theADUsers: any = await client.api(`/groups/${GUID}/members/microsoft.graph.user`)
      .header('ConsistencyLevel', 'eventual')
      //.search('displayName:P')
      .select('displayName,id,mail,surname')
      //.orderby('displayName')
      .get();

      console.log('theADUsers...');
      console.log(theADUsers);



{
    "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users(displayName,id,mail,surname)",
    "value": [
        {
            "displayName": "Doe, John",
            "id": "GUID",
            "mail": "John.Doe@somewhere.com",
            "surname": "Doe"
        },
        {
            "displayName": "Doe, John",
            "id": "GUID",
            "mail": "John.Doe2@somewhere.com",
            "surname": "Doe"
        },
        {
            "displayName": "Doe, John",
            "id": "GUID",
            "mail": "John.Doe3@somewhere.com",
            "surname": "Doe"
        },

Solution

  • You can do something like:

    theADUsers.value.forEach(user => console.log(user));
    

    The result should be already parsed and you do not need JSON parsing. For reference look here: https://learn.microsoft.com/en-us/sharepoint/dev/spfx/use-msgraph