Search code examples
c#microsoft-graph-apimicrosoft-graph-sdks

Unable to retreive user properties (collections like contacts)


I'm trying to modify a user his outlook contacts, however im unable to even obtain them using the graph client. The application is basically a service which authenticates using a client<>secret. Am I missing out on permissions or am i approving it the wrong way?

I tried the following code:

IConfidentialClientApplication cca = ConfidentialClientApplicationBuilder
                .Create(Startup.Configuration["AppConfiguration:Office365:ClientId"])
                .WithClientSecret(Startup.Configuration["AppConfiguration:Office365:ClientSecret"])
                .WithTenantId(Startup.Configuration["AppConfiguration:Office365:TenantId"])
                .Build();

ClientCredentialProvider ccp = new ClientCredentialProvider(cca);
_confidentialClient = new GraphServiceClient(ccp);

User user = await _confidentialClient.Users["someone@something.com"]
    .Request()
    .Select(u => new
    {
        u.Id,
        u.Contacts
    })
    .Expand(u => new
    {
        u.Contacts
    })
    .GetAsync();

Solution

  • To list user contacts you have to call:

    https://graph.microsoft.com/v1.0/users/{id}/contacts
    

    Graph client:

    await _confidentialClient.Users["someone@something.com"].Contacts.Request().GetAsync();
    

    If you want to modify contacts check documentation:

    Create contact

    Update contact