Rohit Kumar
Rohit Kumar

Reputation: 89

Retrieve contacts along with profile pic in batches using Graph API

With the query below, we can get contact's profile pictures using Microsoft Graph:

https://graph.microsoft.com/v1.0/users/{user-name}/contacts/{id}/photo/$value

Using the above query is taking a bit longer time to retrieve a large number of contacts. Is there any way to retrieve the contacts along with the profile picture in batches?

With the help the of below batch request, the contacts are fetched successfully in batches of 20(max), but for the profile photo it returns body. I am not able to process this body content. Is there any way out to process this body into image format which can be processed.

Any C# API, if there, will be much helpful.

Batch_Request

Below is the response. The body content is not of much help. How to process it?

Response

Upvotes: 2

Views: 881

Answers (1)

Stanley Gong
Stanley Gong

Reputation: 12153

The body here is the content of a photo but base64 encoded. You can save it as a file directly.

This is my demo data from Graph API response :

enter image description here

Use the c# code below to save it as an image :

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            var imgBody = "<body value here>";
            byte[] bytes = Convert.FromBase64String(imgBody);
            using (Image image = Image.FromStream(new MemoryStream(bytes)))
            {
                image.Save("d:/test.jpg", ImageFormat.Jpeg);
            }
        }
    }
}

Result : enter image description here enter image description here

Upvotes: 1

Related Questions