Parse a graph url to get route (users, places)

Viewed 40

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?

1 Answers

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();

}
Related