How can i export contacts from public folder?

Copper Contributor

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?

6 Replies

Hi @Michael_Kolowicz,

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:

  1. Application Registration:

    • Start by registering your application in Azure AD. This registration process will involve specifying the required permissions to access the public folder. Ensure you grant the appropriate permissions based on your needs.
  2. Authentication:

    • Employ a library like the Microsoft Authentication Library (MSAL) for .NET Core to manage the authentication process. This will allow you to obtain an access token using your application's credentials.
  3. Access the Public Folder:

    • Utilize the Microsoft Graph API, specifically the Outlook API, to access the contacts within the public folder. This API allows you to interact with the contacts stored there.
  4. Export Contacts to CSV:

    • Retrieve the contacts from the public folder and format the data for export to a CSV file. You can make use of the standard System.IO namespace in .NET Core to create and write data to a CSV file.
  5. Scheduling:

    • To run your application at regular intervals, consider using a scheduling mechanism like the Windows Task Scheduler or implement recurring jobs using a library like Hangfire.

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 = $"{tenantId}";
var scopes = new string[] { "" };

var app = PublicClientApplicationBuilder.Create(clientId)
    .WithRedirectUri("http://localhost") // Redirect URI for console apps

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


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?



Graph has no support for Public folders, likely never will. EWS should work, here's a sample script that enumerates items in PF:
Modify it to do the export to CSV or whatever is needed.
Thank you - i think that will be helpfull. But i missing the 2MF / Modern-Authetication in this PS. Our goal is to run this "script/tool" on the server every hour and download the contacts to csv. We didn´t want do it manualy/ by hand.

I have create a Application in Azure like them mentioned in the first answer post.
Just replace the connectivity part with your preferred method, EWS supports OAuth so you can use all the standard flows:
Which Nuget-Project i must import to access from .NET5/.NET6 or .NET7 to use EWS. The Microsoft.Exchange.WebServices is not useable for this newer Versions