Idra
Idra

Reputation: 5797

MS Graph API returns 500 when I try to request groups

So I am developing a sharepoint webpart where I need to check if a user is in an AD group. Now here is the odd part the MS Graph query I need to you for that is:

https://graph.microsoft.com/v1.0/me/transitiveMemberOf/microsoft.graph.group?$count=true

And that is the exact Query that my WP sends out, but it returns a 500 error message. Now I thought I had permissions missing, but the Explorer says I do not need any.

Here is my GraphService that handles MS Graph:

import { MSGraphClient } from '@microsoft/sp-http';
import * as MicrosoftGraph from '@microsoft/microsoft-graph-types';
import { WebPartContext } from "@microsoft/sp-webpart-base";

/**
 * The class that handles the MS Graph API calls
 */
export default class GraphService {
    /**
     * The MS Graph client with does the calls
     */
    private _client:MSGraphClient;

    /**
     * Sets the client for the Graph API, needs to be called before the class can be used
     * @param context The context of the WP
     */
    public async setClient(context:WebPartContext) {
        this._client = await context.msGraphClientFactory.getClient();
    }
    /**
     * Checks to see if a user belongs in a user group or not
     * @param groupName The group name with we want to check
     */
    public async checkCurrentUserGroup(groupName:string) {
        const rawData = await this._client.api('/me/transitiveMemberOf/microsoft.graph.group').count(true).get();
        const arr = rawData.value.filter(i => i.displayName == groupName);
        return !!arr.length;
    }


    /**
     * Returns the users from AD who have a manager and whom companyName is a specific company name
     * @param companyName The company name to whom the users need to belong to
     */
    public async getUsers(companyName:string):Promise<any[]> {
        const rawData = await this._client.api('/users').filter('accountEnabled eq true').expand('manager').select('displayName,companyName,mail,department,jobTitle').get();

        //need to do manual filtering, becuase you can't user filter on the "companyName" field and you can't search and expand at the same time without returing everything
        const filteredData = rawData.value.filter(i => i.companyName == companyName && i.manager);

        //remaps the data to IUserModel
        const ret:any[] = filteredData.map(i => <any>{
            name: i.displayName,
            id: 0,
            email: i.mail,
            manager: i.manager.displayName,
            managerEmail: i.manager.mail,
            managerId: 0,
            hasDiscussionForm: false,
            department: i.department,
            position: i.jobTitle
        });

        return ret;
        
    } 
}

The problem then is that the method checkCurrentUserGroup returns 500.

I've given the following permissions to the wp:

The getUsers method works as expected.

In the MS Graph Explorer the query works just fine. What am I missing?

Upvotes: 0

Views: 400

Answers (1)

Sruthi J
Sruthi J

Reputation: 1602

According to the document the request header must have ConsistencyLevel and eventual header and please make sure to pass the request header and please refer to this document for more information

let res = await client.api('/users/{id}/transitiveMemberOf/microsoft.graph.group')
    .header('ConsistencyLevel','eventual')
    .search('displayName:tier')
    .select('displayName,id')
    .orderby('displayName ')
    .get();

Upvotes: 2

Related Questions