Call Microsoft 365 services in Visual Studio 2017 with the Microsoft Graph API

You can use the Connected Services in Visual Studio to configure your app to call the Microsoft Graph API. This article describes how to get a signed in user's profile photo, upload it to OneDrive, and send an email with a sharing link to the photo.

Get set up

To use the Office 365 Connected Services with Microsoft Graph, you'll need to do the following:

  • Download the Visual Studio 2017 Preview, if you haven't already. If you're using an earlier version of Visual Studio, you can use Visual Studio 2017 Preview side by side with your current version.

  • Get a Microsoft 365 subscription. To get a free trial, join the Microsoft 365 Developer program.

Get the starter project

Download the Microsoft Graph ASP.NET Connected Services Sample. This sample includes the references that you need to authenticate against Microsoft Graph. After you download the starter project, unzip, and open the sample in Visual Studio 2017 Preview.

Add the Connected Service

You're now ready to add the Microsoft Graph service to your Visual Studio project.

  1. In Solution Explorer, choose Connected Services to open the Connected Services tab.

  2. Choose the Access Microsoft 365 services with Microsoft Graph provider. Follow the wizard. Select the following permissions (you can change the permissions later):

    • For the File APIs, set permissions to Have full access to your files.
    • For the Mail APIs, set permissions to Send mail as you.
    • For the User APIs, set permissions to Sign you in and read your profile.

Call the Microsoft Graph API

The starter sample is configured to send a simple email. You can use Microsoft Graph to update the sample to send an email with a link to the signed-in user's profile photo in OneDrive.

  1. Go to 'Models\GraphService.cs', which hosts the code to call Microsoft Graph.

  2. Find and Uncomment calls to the SDK in the following methods. This shows how to call Microsoft Graph to get a profile photo, upload a file to OneDrive, and get a sharing link.

        GetCurrentUserPhotoStream(GraphServiceClient graphClient)
        UploadFileToOneDrive(GraphServiceClient graphClient, byte[] file)
        GetSharingLink(GraphServiceClient graphClient, string Id)

Tip: Each comment starts with '//Uncomment:'

Run the sample

Build and run the sample. Next, choose the Sign-in link on the top right, and then choose Get email address followed by Send email.

This will send an email that includes a link to your profile photo.


  • If you stop and rerun the sample from Visual Studio, you might need to explicitly sign out for the sample to work.
  • If you get an exception that indicates that the User is not authenticated, you might need to repeat the Add the Connected Service step.
  • Make sure to sign in using an account in the same domain as the one you selected in the Select Domain step of the wizard.

Explore the code

You can now use Visual Studio 2017 to connect to and configure your services. The starter sample creates the scaffolding and references for you.

The starter sample includes the following files:

Need help?

If you need help, post your questions on Microsoft Q&A. Tag your post with {microsoft-graph-identity}.