This project is being archived and replaced with the Build Xamarin apps with the Microsoft Graph .NET SDK training. As part of the archival process, we're closing all open issues and pull requests.
You can continue to use this sample "as-is", but it won't be maintained moving forward. We apologize for any inconvenience.
- Introduction
- Prerequisites
- Register and configure the app
- Build and debug
- Questions and comments
- Additional resources
This sample shows how to connect a Xamarin Forms app to a Microsoft work or school (Azure Active Directory) or personal (Microsoft) account using the Microsoft Graph API to retrieve a user's profile picture, upload the picture to OneDrive, and send an email that contains the photo as an attachment and the sharing link in its text. It uses the Microsoft Graph .NET Client SDK to work with data returned by Microsoft Graph.
In addition, the sample uses the Microsoft Authentication Library (MSAL) for authentication. The MSAL SDK provides features for working with the Azure AD v2.0 endpoint, which enables developers to write a single code flow that handles authentication for both users' work or school and personal accounts.
If you'd like to work with MSAL in a Xamarin Forms app of your own, follow these instructions for setting up a Xamarin Forms project with MSAL.
This library is suitable for use in a production environment. We provide the same production level support for this library as we do our current production libraries. During the preview we may make changes to the API, internal cache format, and other mechanisms of this library, which you will be required to take along with bug fixes or feature improvements. This may impact your application. For instance, a change to the cache format may impact your users, such as requiring them to sign in again. An API change may require you to update your code. When we provide the General Availability release we will require you to update to the General Availability version within six months, as applications written using a preview version of library may no longer work.
This sample requires the following:
- Visual Studio 2015
- Xamarin for Visual Studio
- Windows 10 (development mode enabled)
- Either a Microsoft or Office 365 for business account
If you want to run the iOS project in this sample, you'll need the following:
- The latest iOS SDK
- The latest version of Xcode
- Mac OS X Yosemite(10.10) & above
- Xamarin.iOS
- A Xamarin Mac agent connected to Visual Studio
You can use the Visual Studio Emulator for Android if you want to run the Android project.
-
Sign into the App Registration Portal using either your personal or work or school account.
-
Select Add an app.
-
Enter a name for the app, and select Create.
The registration page displays, listing the properties of your app.
-
Under Platforms, select Add platform.
-
Select Native Application.
-
Copy the Application Id value and the Custom Redirect URI value (under the Native Application header) that was created for you when you added the Native Application platform. This URI should contain your Application Id value and be in this form:
msal<Application Id>://auth
You'll need to enter these values into the sample app.The app id is a unique identifier for your app.
-
Select Save.
Note: If you see any errors while installing packages during step 12, make sure the local path where you placed the solution is not too long/deep. Moving the solution closer to the root of your drive resolves this issue.
-
Open the App.cs file inside the XamarinConnect (Portable) project of the solution.
-
After you've loaded the solution in Visual Studio, configure the sample to use the client id that you registered by making this the value of the ClientId variable in the App.cs file.
-
Open the UserDetailsClient.iOS\info.plist file in a text editor. Unfortunately you can't edit this file in Visual Studio. Locate the
<string>msalENTER_YOUR_CLIENT_ID</string>
element underCFBundleURLSchemes
key. -
Replace
ENTER_YOUR_CLIENT_ID
with the application id value that you got when you registered your app. Be sure to retainmsal
before the application id. The resulting string value should look like this:<string>msal<application id></string>
. -
Open the XamarinConnect.Droid\Properties\AndroidManifest.xml file. Locate this element:
<data android:scheme="msalENTER_YOUR_CLIENT_ID" android:host="auth" />
. -
Replace
ENTER_YOUR_CLIENT_ID
with the application id value that you got when you registered your app. Be sure to retainmsal
before the application id. The resulting string value should look like this:<data android:scheme="msal<application id>" android:host="auth" />
. -
Select the project that you want to run. If you select the Universal Windows Platform option, you can run the sample on the local machine. If you want to run the iOS project, you'll need to connect to a Mac that has the Xamarin tools installed on it. (You can also open this solution in Xamarin Studio on a Mac and run the sample directly from there.) You can use the Visual Studio Emulator for Android if you want to run the Android project.
-
Press F5 to build and debug. Run the solution and sign in with either your personal or work or school account.
Note You might have to open the Build Configuration Manager to make sure that the Build and Deploy steps are selected for the UWP project.
UWP | Android | iOS |
---|---|---|
The code in the main page of the app is relatively straight-forward and self-explanatory, as the calls for authentication and email service actually occur in the helper classes. The main page code primarily consists of event handlers for the two buttons:
-
OnSignInSignOut
When the Text value of this button is "connect," this method calls the GetAuthenticatedClient method to acquire a GraphServicesClient object representing the current user, which it uses to set user email address and display name. If this is successful, it also enables the send mail button and the text box where the user can enter an email address, and populates that text box with the user's own email address.
-
MailButton_Click
This method calls the ComposeAndSendMailAsync method, using the email address and display name variables set during ConnectButton_Click. If this method call is successful, it also updates the UI text accordingly.
With that in mind, it's worth looking at two methods in the helper classes in a little more detail:
-
GetAuthenticatedClient
This method of the AuthenticationHelper class authenticates the user with the Microsoft Authentication Library (MSAL).
It does this by retrieving an authentication token from an MSAL PublicClientApplication object and then passing that token to a Microsoft Graph DelegateAuthenticationProvider object.
The SignInCurrentUserAsync method on the main page can then read the user from this GraphServicesClient object and set the user email address and display name.
-
ComposeAndSendMailAsync
This method of the MailHelper class composes and sends the sample email.
If you'd like to contribute to this sample, see CONTRIBUTING.MD.
This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.
We'd love to get your feedback about the Microsoft Graph Connect Sample for Xamarin Forms project. You can send your questions and suggestions to us in the Issues section of this repository.
Your feedback is important to us. Connect with us on Stack Overflow. Tag your questions with [MicrosoftGraph].
- Other Microsoft Graph Connect samples
- Microsoft Graph overview
- Office developer code samples
- Office dev center
Copyright (c) 2016 Microsoft. All rights reserved.