Forum Discussion

Michael_Kolowicz's avatar
Michael_Kolowicz
Copper Contributor
Oct 18, 2023

How can i export contacts from public folder?

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?

  • LeonPavesic's avatar
    LeonPavesic
    Silver Contributor

    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 = $"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)

    • Michael_Kolowicz's avatar
      Michael_Kolowicz
      Copper Contributor

      LeonPavesic 

      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?

       

       

Resources