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?
Specifying request headers using C# Office Graph SDK client
Asked Answered
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();
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
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");});
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 Review –
Ammo
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.