Parse a graph url to get search, filter, select parameters

Viewed 65

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
                        .Request(queryOptions)
                        .Header("ConsistencyLevel", "eventual")
                        .Filter("")
                        .Select("")
                        .OrderBy("")                        
                        .GetAsync();
    });
}

an example of string s is "https://graph.microsoft.com/v1.0/users?$count=true&$search="displayName:room"&$filter=endsWith(mail,'microsoft.com')&$orderBy=displayName&$select=id,displayName,mail"

how do I parse this string so that I can pass:

displayName:room as Search value

endsWith(mail,'microsoft.com') as Filter value

displayName and OrderBy value

id,displayName,mail as Select value

to the code?


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

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

            return await _graphServiceClient
                                .Users
                                .Request(queryOptions)
                                .Header("ConsistencyLevel", "eventual")
                                .Filter("endsWith(mail,'microsoft.com')")
                                .Select("id,displayName,mail")
                                .OrderBy("displayName")                                
                                .GetAsync();
}

UPDATE:

Same for places, here is what I have tried based on the answer below:

            var queryParamValues = HttpUtility.ParseQueryString(url, Encoding.UTF8);            
            var filterValue = queryParamValues["$filter"];      
            var selectValue = queryParamValues["$select"];

            var queryOptions = new List<QueryOption>() { new QueryOption("$count", "true") };         
            var roomUrl = _graphServiceClient.Places.AppendSegmentToRequestUrl("microsoft.graph.room");
            var request = new GraphServicePlacesCollectionRequest(roomUrl, _graphServiceClient, queryOptions);
            if (!string.IsNullOrEmpty(filterValue)) { request = (GraphServicePlacesCollectionRequest)request.Filter(filterValue); }            
            if (!string.IsNullOrEmpty(selectValue)) { request = (GraphServicePlacesCollectionRequest)request.Select(selectValue); }

            return await request
                            .Top(MaxResultCount)
                            .GetAsync();
1 Answers

Use HttpUtility.ParseQueryString method and read value of each query parameter.

var url = "https://graph.microsoft.com/v1.0/users?$count=true&$search=\"displayName:room\"&$filter=endsWith(mail,'microsoft.com')&$orderBy=displayName&$select=id,displayName,mail";

var queryParamValues = HttpUtility.ParseQueryString(url, Encoding.UTF8);
var searchValue = queryParamValues["$search"];
var filterValue = queryParamValues["$filter"];
var orderValue = queryParamValues["$orderBy"];
var selectValue = queryParamValues["$select"];

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

// if search parameter not missing in url
if(!string.IsNullOrEmpty(searchValue))
{
    queryOptions.Add(new QueryOption("$search", searchValue));
}

// create base request
var request =  _graphServiceClient
                        .Users
                        .Request(queryOptions)
                        .Header("ConsistencyLevel", "eventual");

// if filter parameter not missing in url
if(!string.IsNullOrEmpty(filterValue))
{
    request = request.Filter(filterValue);
}

// if orderby parameter not missing in url
if(!string.IsNullOrEmpty(orderValue))
{
    request = request.OrderBy(orderValue);
}

// if select parameter not missing in url
if(!string.IsNullOrEmpty(selectValue))
{
    request = request.Select(selectValue);
}
        
return await request 
             .Top(MaxResultCount)
             .GetAsync();
Related