list contacts MS Graph

Jürgen Keitzl 41 Reputation points
2022-03-29T16:00:13.827+00:00

HI,

I trying to learn Xamarin or better learn to code and of course I challenge me to connect an app to Office365 Accounts and fetch contacts, Mails or Tasks of a user.

I read and use all the docs, but now I have a problem where I see my limitations. I try to fetch contacts from the user account.

On the one side I have my data model which contains the properties for the contact.

On the other side I get the contacts from Graph with:

 var mscontacts = await App.GraphClient.Me
                 .Contacts
                 .Request()
                 .GetAsync();

If I try to fill my ObservableCollection based on my model, with the data from MS Graph then I get an error. So, I understand from Graph I get a Json stream(?) which is not compatible to my model. Is that correct so far?

What I must do now? Do I need a converter? I my approach completely wrong? Are there any books, wich describe that?

I'm thankful for your help.

Juergen

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,206 questions
Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
9,119 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 23,306 Reputation points Microsoft Vendor
    2022-03-30T07:10:54.017+00:00

    Hello,

    If I try to fill my ObservableCollection based on my model, with the data from MS Graph then I get an error. So, I understand from Graph I get a Json stream(?) which is not compatible to my model. Is that correct so far?

    If the response is in JSON format, you need to parse this JSON String, try to use NewtonSoft JSON.NET library.
    Right-click your project, select Manage Nuget Packages, search NewtonSoft under Browse, install this package, then you could deserialize the JSON, refer to

     var mscontacts = "{\"value\":[{\"parentFolderId\":\"parentFolderId-value\",\"birthday\":\"datetime-value\",\"fileAs\":\"fileAs-value\",\"displayName\":\"displayName-value\",\"givenName\":\"givenName-value\",\"initials\":\"initials-value\"}]}";  
    
                var mscontactsModel = JsonConvert.DeserializeObject<Mscontacts>(mscontacts);// I define a faker data to test  
                foreach (var mscontact in mscontactsModel.value)  
                {  
                    Console.WriteLine("{0}", mscontact.initials);  
                }  
    

    The Model :

      public class Mscontacts  
            {  
                public ObservableCollection<MscontactModel> value { get; set; }   
            }  
        public class MscontactModel  
        {  
            public string parentFolderId { get; set; }  
            public string birthday { get; set; }  
            public string fileAs { get; set; }  
            public string displayName { get; set; }  
            public string givenName { get; set; }  
            public string initials { get; set; }  
        }  
    

    You could refer to the Retrieve data part of this doc about How to Consume a RESTful web service: https://learn.microsoft.com/en-us/xamarin/xamarin-forms/data-cloud/web-services/rest#retrieve-data

    Best Regards,
    Wenyan Zhang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments