SDKs overview

OneDrive provides developers with Software Development Kits (SDKs) and libraries to integrate its cloud storage and file management capabilities into various applications. These SDKs typically wrap the Microsoft Graph API, which serves as a unified endpoint for accessing data across Microsoft 365 services, including OneDrive, Outlook, Azure Active Directory, and more. The Microsoft Graph API uses RESTful principles and OAuth 2.0 for authentication and authorization, allowing applications to manage user files, folders, and sharing permissions within OneDrive.

Integrating with OneDrive via an SDK enables applications to perform operations such as uploading and downloading files, creating and managing folders, sharing documents, and synchronizing content. The choice of SDK depends on the target platform and programming language, with official support generally available for common environments like mobile (iOS, Android), desktop (Universal Windows Platform), and web (.NET, JavaScript).

The developer experience for OneDrive is largely channeled through the Microsoft Graph developer portal, which provides extensive documentation, code samples, and tools for building integrated applications. Developers can utilize these resources to understand API endpoints, authentication flows, and best practices for interacting with OneDrive data.

Official SDKs by language

Microsoft offers several official SDKs and libraries to facilitate integration with OneDrive through the Microsoft Graph API. These SDKs are designed to simplify common development tasks, handle authentication, and abstract away the complexities of direct HTTP requests to the REST API.

Language/Platform SDK Name Package Manager/Repository Maturity
.NET (C#) Microsoft Graph SDK for .NET NuGet (Microsoft.Graph) Stable
JavaScript Microsoft Graph JavaScript SDK npm (@microsoft/microsoft-graph-client) Stable
Java Microsoft Graph SDK for Java Maven/Gradle (com.microsoft.graph:microsoft-graph) Stable
iOS (Swift/Objective-C) Microsoft Graph SDK for iOS CocoaPods (MicrosoftGraphSDK) Stable
Android (Java/Kotlin) Microsoft Graph SDK for Android Gradle (com.microsoft.graph:microsoft-graph-core) Stable

Each SDK provides language-specific client libraries that allow developers to make calls to the Microsoft Graph API with familiar programming constructs. This includes strongly typed objects for API responses, helper methods for common operations, and integrated authentication flows using Azure Active Directory.

Installation

Installation procedures vary depending on the chosen SDK and development environment. Below are common installation methods for the primary official SDKs.

.NET SDK

For .NET applications, the Microsoft Graph SDK is distributed via NuGet. Developers can install it using the NuGet Package Manager Console or the .NET CLI.

NuGet Package Manager Console

Install-Package Microsoft.Graph

.NET CLI

dotnet add package Microsoft.Graph

Further details on installing the .NET SDK are available in the Microsoft Graph .NET SDK installation guide.

JavaScript SDK

The Microsoft Graph JavaScript SDK is typically installed using npm, the package manager for Node.js.

npm

npm install @microsoft/microsoft-graph-client

For browser-based applications, the SDK can also be included via a CDN or bundled using tools like Webpack. Comprehensive instructions are provided in the Microsoft Graph JavaScript SDK documentation.

Java SDK

For Java projects, the Microsoft Graph SDK is integrated using Maven or Gradle.

Maven

<dependency><groupId>com.microsoft.graph</groupId><artifactId>microsoft-graph</artifactId><version>5.x.x</version></dependency>

Gradle

implementation 'com.microsoft.graph:microsoft-graph:5.x.x'

Ensure to replace 5.x.x with the latest stable version. Detailed setup steps can be found in the Microsoft Graph Java SDK installation instructions.

iOS SDK

The Microsoft Graph SDK for iOS (Swift/Objective-C) is distributed through CocoaPods.

Podfile

pod 'MicrosoftGraphSDK'

After adding this to your Podfile, run pod install. Refer to the Microsoft Graph iOS SDK documentation for complete installation and configuration.

Android SDK

For Android development (Java/Kotlin), the Microsoft Graph SDK components are added to your Gradle dependencies.

Gradle

implementation 'com.microsoft.graph:microsoft-graph:5.x.x'

Replace 5.x.x with the current version. The Microsoft Graph Android SDK setup guide provides further details.

Quickstart example

This quickstart example demonstrates how to authenticate and list a user's OneDrive files using the Microsoft Graph .NET SDK. Before running this code, you need to register an application in Azure Active Directory and obtain a client ID and tenant ID. You will also need to configure appropriate API permissions (e.g., Files.Read) for your application.

Prerequisites:

  • .NET 6.0 or later
  • Microsoft.Graph NuGet package installed
  • Azure AD app registration with Files.Read permission

C# Code Example (Console Application)

using Azure.Identity;
using Microsoft.Graph;
using System;
using System.Threading.Tasks;

public class OneDriveQuickstart
{
    private static string _clientId = "YOUR_CLIENT_ID"; // Replace with your Azure AD application's client ID
    private static string _tenantId = "YOUR_TENANT_ID"; // Replace with your Azure AD tenant ID

    public static async Task Main(string[] args)
    {
        // Configure the authentication provider for public client applications
        var options = new DeviceCodeCredentialOptions
        {
            ClientId = _clientId,
            TenantId = _tenantId,
            AuthorityHost = AzureAuthorityHosts.AzurePublicCloud,
            // Callback function that will be called during the interactive authentication flow
            DeviceCodeCallback = (deviceCodeResult) =>
            {
                Console.WriteLine(deviceCodeResult.Message);
                return Task.FromResult(0);
            },
        };

        var credential = new DeviceCodeCredential(options);
        var graphClient = new GraphServiceClient(credential);

        try
        {
            Console.WriteLine("Authenticating...");
            // Get the current user's drive items (files and folders in root)
            var driveItems = await graphClient.Me.Drive.Root.Children
                .Request()
                .GetAsync();

            Console.WriteLine("Successfully authenticated and retrieved drive items.");
            Console.WriteLine("\nOneDrive Root Items:");
            foreach (var item in driveItems.CurrentPage)
            {
                Console.WriteLine($"- {item.Name} ({item.Size ?? 0} bytes) [{(item.Folder != null ? "Folder" : (item.File != null ? "File" : "Unknown"))}]");
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Error: {ex.Message}");
            // For more detailed error information, consider logging ex.ToString()
            if (ex is ServiceException serviceException)
            {
                Console.WriteLine($"Service Error Code: {serviceException.Error.Code}");
                Console.WriteLine($"Service Error Message: {serviceException.Error.Message}");
            }
        }

        Console.WriteLine("\nPress any key to exit.");
        Console.ReadKey();
    }
}

This code uses the DeviceCodeCredential for authentication, which is suitable for console applications requiring user interaction. For web or desktop applications, other credential flows like InteractiveBrowserCredential or ClientSecretCredential might be more appropriate. Always ensure secure handling of client IDs and other sensitive credentials.

Community libraries

While Microsoft provides official SDKs, the broader developer community also contributes libraries and tools that interact with OneDrive, often leveraging the underlying Microsoft Graph API. These community-driven projects can offer alternative language bindings, specialized utilities, or integrations with frameworks not directly supported by official SDKs.

For instance, developers working with Python might use the O365-REST-Python-SDK, a community-maintained library that provides a Pythonic wrapper around the Microsoft Graph API, including OneDrive functionalities. Similarly, there are various open-source projects on platforms like GitHub that demonstrate or facilitate OneDrive integration in different programming contexts.

When considering community libraries, it is advisable to evaluate their maintenance status, community support, and alignment with official API changes. While they can offer flexibility, official SDKs generally provide the most robust and up-to-date integration experience due to direct support from Microsoft. Developers should consult project repositories and documentation for specific community libraries to assess their suitability for a given project, such as those found on Microsoft's GitHub organization for Graph or broader search results on GitHub for 'OneDrive API' or 'Microsoft Graph client'.

The OAuth 2.0 authorization framework is a critical component for all integrations, official or community-driven, ensuring secure access to user data without sharing credentials directly.