Search code examples
c#jsonmicrosoft-graph-apimicrosoft-teams-js

How can I parse the JSON response from Microsoft Graph Api List Chat into the textbox


This is the code what I have tried.

Based on my limited knowledge it cannot display JSON response of Microsoft.Graph API, it just appears the message box with the content of Microsoft.Graph.User.

How can I parse the JsonResponse into the textbox.

Documentation of ListChat:

https://learn.microsoft.com/en-us/graph/api/chat-list?view=graph-rest-1.0

private async void button2_Click(object sender, EventArgs e)
{
    var scopes = new[] { "Chat.ReadBasic", "Chat.ReadWrite", "Chat.Read" };

    // Multi-tenant apps can use "common",
    // single-tenant apps must use the tenant ID from the Azure portal
    var tenantId = "5xxxx3-3xxa-4xxx1-9xx0c-exxxb0";
    // Value from app registration
    var clientId = "35xxxx-5xxx2-4xx9-8500-63xxxxaf";

    // using Azure.Identity;
    var options = new TokenCredentialOptions
    {
        AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
    };

    var userName = "xx.xxxx@xxxi.com";
    var password = "Axxxxx@";

    // https://learn.microsoft.com/dotnet/api/azure.identity.usernamepasswordcredential
    var userNamePasswordCredential = new UsernamePasswordCredential(
                userName, password, tenantId, clientId, options);
    GraphServiceClient graphClient = new GraphServiceClient(userNamePasswordCredential, scopes);

    var request = await graphClient.Me.Request().GetAsync();
    String txt = request.ToString();
    MessageBox.Show(txt);
}

In Correct Response


Solution

  • You can try to deserialize it with Newtonsoft.Json

    https://www.nuget.org/packages/Newtonsoft.Json/

    To do that you need a class with the same properties as the .json file. So something like:

    public class Response
    {
        public List<Value> Values {get;set;}
    }
    
    public class Value
    {
        public string Id {get;set;}
        public string Topic {get;set;
        .....
        public ChatViewPoint ChatViewPoint {get;set;}
    }
    public class ChatViewPoint
    {
        public bool IsHidden {get;set;}
        ....
    }
    

    After you created the classes you can try to deserialize the string to the given datamodel.

    Hope it works