Specifying request headers using C# Office Graph SDK client
Asked Answered
V

2

4

I am interacting with Office graph using C# Office Graph SDK. I need to create specific request header, while I am using GraphServiceClient and RequestBuilder to create the request. How can I achieve that?

Vena answered 14/1, 2019 at 14:37 Comment(0)
V
5

To set custom header for the request, you need to create Option collection. To add headers, you need to add new item of type HeaderOption to this collection. See code sample bellow:

List<Option> options = new List<Option>
{
   //Creating query parameters
   new QueryOption("filter", $"(start/dateTime le '{DateTime.Now.AddMinutes(5).ToString("yyyy-MM-ddTHH:mm")}')),

   //Creating header
   new HeaderOption("Prefer","outlook.timezone=\"Europe/Budapest\"")
};

var res = await graphClient.Users["userId"].Events.Request(options).GetAsync();
Vena answered 14/1, 2019 at 14:37 Comment(1)
I discovered through trial and error that this is the better way to do this as it allows the ability to write functional unit tests that mock the graph client. The alternative way is to use the Header in the chain of the call after the request.Jurgen
A
4

In the MSGraph v5 SDK and there is no Microsoft.Graph.Option class nor is there a Request method. Headers are added using the requestConfiguration modifier as follows

var message = await graphServiceClient.Me.Messages["message-id"] .GetAsync((requestConfiguration) =>{requestConfiguration.Headers.Add("Etag", "etag"); requestConfiguration.Headers.Add("If-Match", "ifmatch");});

Apriorism answered 25/4, 2023 at 13:3 Comment(3)
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From ReviewAmmo
That worked for me. Thank you!Bursiform
My first thank you! Nice to help someone else for a change.Apriorism

© 2022 - 2025 — McMap. All rights reserved.