Search code examples
c#microsoft-graph-apimicrosoft-graph-groups

Append a Stringsegment to a MS Graph Request with GraphServiceClient


Is it possible to append a segment to a MS Graph GraphServiceClient Request and fetch that resource?

The scenario: I want to get the root site of a group (more specifically its weburl property)

https://graph.microsoft.com/v1.0/groups/{group-id}/sites/root

but it is not possible to append the /root segment with the QueryBuilder and enumerating sites is not allowed and throws an exception

var task = graphClient.Groups[group.Id].Sites.Request().GetAsync() // exception

I can get the string for the request

var url = graphClient.Groups[group.Id].Sites.Request().AppendSegmentToRequestUrl("root")

But then I would need a method to which I can supply a full Graph Url, for example:

graphClient.MakeRequest(url).GetAsync()

I know I could use the HttpClient Class but that would introduce a different pattern to fetch Graph Resources and I would like to avoid that.

Edit - Solution

Seems as if you have to play with the RequestBuilders that are available under the Microsoft.Graph namespace until you find one that matches the type of your request, all the others return null.

var requestBuilder = client.Groups["guid-of-group"].Sites;
var url = requestBuilder.AppendSegmentToRequestUrl("root");

GroupRequestBuilder builder = new GroupRequestBuilder(url, client);
var result = await builder.Request().GetAsync();

Solution

  • Perharps you could try something like this and pass in the graphServiceClient and created url to a new instance of a request builder.

    
    // create the url from the builders
    var requestBuilder = graphClient.Groups["groupId"].Sites;
    var url = request.AppendSegmentToRequestUrl("root");
    
    // we have to create a new builder as the url property cannot be set/modified publicly
    GroupSitesCollectionRequestBuilder groupSitesCollectionRequestBuilder = new GroupSitesCollectionRequestBuilder(url, graphClient);
    
    // Make the call
    var result = await groupSitesCollectionRequestBuilder.Request().GetAsync();