0

I have the following code in C# to make a graph call:

public async Task<IGraphServiceUsersCollectionPage> GetResponseAsync(string s)
{            

    var queryOptions = new List<QueryOption>()
    {
        new QueryOption("$count", "true"),
        new QueryOption("$search", "")
    };

    return await graphServiceClient
                        .Users // or .Places
                        .Request(queryOptions)
                        .Header("ConsistencyLevel", "eventual")
                        .Filter("")
                        .Select("")
                        .OrderBy("")                        
                        .GetAsync();
    });
}

some examples of string s are:

"https://graph.microsoft.com/v1.0/users"

"https://graph.microsoft.com/v1.0/places/microsoft.graph.room"

how do I parse this string so that I can get users or places:

and run _graphServiceClient.Users or await _graphServiceClient.Places accordingly?

user989988
  • 3,006
  • 7
  • 44
  • 91

1 Answers1

1

Simple solution can be to check if string contains users or places.

Then you need two different methods because for users the result will be IGraphServiceUsersCollectionPage and for places IGraphServicePlacesCollectionPage.

Example

public async Task GetResponseAsync(string s)
{
    if (string.Contains("users")
    {
        var users = await GetUsersAsync(s);
    }
    else if(string.Contains("places")
    {
        var places = await GetPlacesAsync(s);
    }
}

public async Task<IGraphServiceUsersCollectionPage> GetUsersAsync(string s)
{            

    var queryOptions = new List<QueryOption>()
    {
        new QueryOption("$count", "true"),
        new QueryOption("$search", "")
    };
    
    return await graphServiceClient
                        .Users
                        .Request(queryOptions)
                        .Header("ConsistencyLevel", "eventual")
                        .Filter("")
                        .Select("")
                        .OrderBy("")                        
                        .GetAsync();
}

public async Task<IGraphServicePlacesCollectionPage> GetPlacesAsync(string s)
{            

    var queryOptions = new List<QueryOption>()
    {
        new QueryOption("$count", "true"),
        new QueryOption("$search", "")
    };

    var roomUrl = graphServiceClient        
                 .Places
                .AppendSegmentToRequestUrl("microsoft.graph.room");
    return await new GraphServicePlacesCollectionRequest(roomUrl, graphServiceClient, queryOptions)
                .Header("ConsistencyLevel", "eventual")
                .Filter("")
                .Select("")
                .OrderBy("")
                .GetAsync();

}
user2250152
  • 14,658
  • 4
  • 33
  • 57
  • Thanks! On running the code you suggested to get places, I see an error mentioned in this https://stackoverflow.com/questions/73679179/notacceptable-error-when-calling-graph-api-from-c-sharp. And it suggests a solution.How do I update the code to use that solution and add query option, header, filter, search, order by? Any suggestions? – user989988 Sep 22 '22 at 08:09
  • 1
    @user989988 Check updated question how to do it. – user2250152 Sep 22 '22 at 08:43
  • Thanks a lot! Quick question though - is there a way to parse the value microsoft.graph.room from the graph url string and pass it to the function instead of hard coding it? – user989988 Sep 22 '22 at 16:46