Oct 17 2023 10:01 PM
I want to create a small application in .NET Core for export all contacts from a contact-folder in our public folder on MSO365 to csv-file. This application should run every hour.
What mechanism is the right way: MSGraph, EWS or something other?
I have read I need "Modern Authentication" while "Basic Authentication" (Username/Password) is not able anymore. Have someone a small example how i can access the public folders in a .NET Core (Console) App?
Oct 18 2023 03:29 AM
To export contacts from a public folder in Office 365 to a CSV file using a .NET Core application, you should utilize the Microsoft Graph API. This API is the recommended and modern way to programmatically access Office 365 data, including public folders.
Here are the key steps to accomplish this task:
Application Registration:
Authentication:
Access the Public Folder:
Export Contacts to CSV:
Scheduling:
Here's a simplified example in .NET Core (C#) demonstrating how to access contacts in a public folder using the Microsoft Graph API:
using Microsoft.Graph;
using Microsoft.Identity.Client;
// Authentication
var clientId = "YOUR_CLIENT_ID";
var clientSecret = "YOUR_CLIENT_SECRET";
var tenantId = "YOUR_TENANT_ID";
var authority = $"https://login.microsoftonline.com/{tenantId}";
var scopes = new string[] { "https://graph.microsoft.com/.default" };
var app = PublicClientApplicationBuilder.Create(clientId)
.WithAuthority(authority)
.WithRedirectUri("http://localhost") // Redirect URI for console apps
.Build();
var result = app.AcquireTokenForClient(scopes).ExecuteAsync().GetAwaiter().GetResult();
var accessToken = result.AccessToken;
// Initialize the GraphServiceClient
var graphClient = new GraphServiceClient(new DelegateAuthenticationProvider((requestMessage) =>
{
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
return Task.CompletedTask;
}));
// Access the public folder's contacts
var contacts = await graphClient.Users["publicFolderMailboxId"].Contacts.Request().GetAsync();
// Export contacts to CSV
foreach (var contact in contacts)
{
// Write contact data to CSV file
}
// Implement the scheduling mechanism to run this code periodically.
Please click Mark as Best Response & Like if my post helped you to solve your issue.
This will help others to find the correct solution easily. It also closes the item.
If the post was useful in other ways, please consider giving it Like.
Kindest regards,
Leon Pavesic
(LinkedIn)
Oct 18 2023 06:01 AM - edited Oct 18 2023 06:17 AM
Thanks for your sample. But i try to make it run in a prototyp-app. But the problem is:
I using MSGraph 5.0 - but there is missing the DelegateAuthenticationProvider.
Can you please make a update for your example with the actual MSGraph?
And please: Where I can find then publicFolderMailboxId? Is it the name of the public folder mailbox (primary hierachy)? Or where is it?
Oct 18 2023 08:06 AM
Oct 18 2023 08:32 AM
Oct 19 2023 12:09 AM
Oct 30 2023 06:27 AM