Exploring Microsoft Graph: Accessing Files and Enhancing Microsoft 365 Platform
Uncover the power of Microsoft Graph through accessing files, extending Microsoft 365 experiences, and utilizing the gateway to Microsoft Cloud data. Learn about authentication options and how to interact with the Microsoft Graph Direct REST API.
Download Presentation
Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
E N D
Presentation Transcript
Accessing Files with Microsoft Graph
Microsoft Graph Overview Agenda Accessing the Microsoft Graph Office 365 groups & Security groups Dynamic membership Accessing files Downloading files
Microsoft 365 Platform Extend Microsoft 365 experiences Build your experience web, device, and service apps Documents Pages Timeline Conversations iOS/Android/Windows/Web Microsoft Graph 1
Microsoft Graph Gateway to your data in the Microsoft-cloud https://graph.microsoft.com Office 365 Windows 10 Enterprise Mobility + Security Users, Groups, Organizations Outlook SharePoint OneDrive Teams Planner Excel OneNote Activities Device Relay Commands Notifications Azure AD Intune Identity Manager Advanced Threat Analytics Advanced Threat Protection Mail, Calendar, Contacts and Tasks Sites and Lists Drives and Files Channels, Messages Tasks and Plans Spreadsheets Notes, and more Identity Management Access Control Synchronization Domains Administrative Units Applications and Devices Advanced Threat Analytics Advanced Threat Protection Alerts Policies and more
Microsoft Graph, gateway to Microsoft 365 Single resource that proxies multiple Microsoft services Simplifies token acquisition and management Eliminates the need to traditional discovery (using me and myorganization ) Allows for easy traversal of objects and relationships
Accessing the Microsoft Graph Direct REST API Any platform Any language Any framework Native SDKs Utilize framework & platform specific implementations Abstracts the details of constructing & processing REST requests over HTTP .NET, iOS, Android, PhP, Ruby, JavaScript, etc.
Authentication Options Azure AD only Separate auth flow supports Azure AD accounts only Azure AD and Microsoft Accounts Converged auth flow supports Azure AD accounts and Microsoft accounts (LiveID - hotmail.com, etc.)
Microsoft Account + Azure AD Many apps want to sign users in from both Microsoft account and Azure AD Single endpoint, OpenID Connect and OAuth 2.0 Single SDK Single end user sign in experience Single app registration experience Works with Microsoft Graph Single API endpoint, business and consumer data
Why integrate with OneDrive file storage in the cloud? Tap into billions of files Store your app's files in a powerful cloud Bring your app straight to users within OneDrive Work with content in formats your app understands Work with file content and metadata without downloading the binary React to file changes
Microsoft Graph Files resource Microsoft Graph exposes two resources when working with files: Drive DriveItem Both objects expose data in the following ways: Properties id & name Facets file & photo References children & thumbnails
Accessing a users OneDrive Access the currently signed-in user s OneDrive: https://graph.microsoft.com/v1.0/me/drive Returns details about the user s OneDrive account OneDrive Consumer (personal)/OneDrive for Business (business) Quota details Access root folder using /drive/root endpoint Returns DriveItems collection of folders/files Determine the DriveItem type with the presence of the following properties: Folder File
Accessing a users OneDrive Microsoft Graph .NET SDK GraphServiceClient graphClient = GetAuthenticatedGraphClient(...); // get user's files & folders in the root var oneDriveRoot = client.Me.Drive.Root .Children .Request() .GetAsync() .Result; // display the results foreach (var driveItem in results) { Console.WriteLine(driveItem.Id + ": " + driveItem.Name); }
Accessing files for users, groups & SharePoint sites Accessing another user s files (provided you have permissions) https://graph.microsoft.com/v1.0/users/{user-id}/drive Accessing files in Office 365 groups https://graph.microsoft.com/v1.0/groups/{group-id}/drive Accessing files in SharePoint Online site collections https://graph.microsoft.com/v1.0/sites/{site-id}/drive
Required permissions for working with files & OneDrive Permissions involved in working with files: Files.Read Files.Read.All Files.ReadWrite Files.ReadWrite.All Files.Read.All (for SharePoint site collections) Files.ReadWrite.All (for SharePoint site collections)
Downloading files from OneDrive The DriveItem resource s content property can us used to access the contents of a file Only DriveItems with the file property can be downloaded content property returns the primary stream of the file GraphServiceClient graphClient = GetAuthenticatedGraphClient(...); var fileStream = graphClient.Me.Drive.Items[fileId].Content.Request().GetAsync().Result; var driveItemPath = Path.Combine(System.IO.Directory.GetCurrentDirectory(), "proposal.docx"); // save stream to the local file var driveItemFile = System.IO.File.Create(driveItemPath); fileStream.Seek(0, SeekOrigin.Begin); fileStream.CopyTo(driveItemFile);
Simple file upload for small files Agenda Large file upload for files > 4 MB
Simple upload uploading files < 4 MB Microsoft Graph supports uploading both small and large files Uploading small files (simple upload), covers files < 4MB Single HTTP request submission to upload the file at once
Uploading files with Microsoft Graph API Creating a new file: HTTP PUT https://graph.microsoft.com/v1.0/me/drive/root:/myNewSmallFile.txt:/content Content-Type: text/plain This is a new small file Updating an existing file: HTTP PUT https://graph.microsoft.com/v1.0/me/drive/items/{item-id}/content Content-Type: text/plain a new small file
Uploading small files with Microsoft Graph .NET SDK // get reference to stream of file in OneDrive var fileName = "myNewSmallFile.txt"; var currentFolder = System.IO.Directory.GetCurrentDirectory(); var filePath = Path.Combine(currentFolder, fileName); // get a stream of the local file FileStream fileStream = new FileStream(filePath, FileMode.Open); // upload the file to OneDrive GraphServiceClient graphClient = GetAuthenticatedGraphClient(...); var uploadedFile = graphClient.Me.Drive.Root .ItemWithPath(fileName) .Content.Request() .PutAsync<DriveItem>(fileStream).Result;
Uploading large files (> 4 MB) Microsoft Graph supports uploading both small and large files Uploading large files, covers files > 4 MB Supports resumable uploads in scenarios where connection drops or upload is paused As file is uploaded, developer can monitor progress to report status to consumer of the app
Start large file upload by creating an upload session Create upload session: HTTP POST https://graph.microsoft.com/v1.0/me/drive/root/createUploadSession Content-Type: application/json { "item": { "name": "largefile.zip"} } Returns the upload session: HTTP/1.1 200 OK Content-Type: application/json { "uploadUrl": "https://sn3302.up.1drv.com/up/fe6987415ace7X4e1eF866337", "expirationDateTime": "2019-12-29T09:21:55.523Z" }
Upload file chunks to the upload session Submit chunks of the file using HTTP PUT Target the upload session endpoint returned in the previous call Content-Range header specifies which chunk you re uploading & total size of the file: HTTP PUT https://sn3302.up.1drv.com/up/fe6987415ace7X4e1eF866337 Content-Length: 26 Content-Range: bytes 0-25/128 <bytes 0-25 of the file> Response: HTTP/1.1 202 Accepted Content-Type: application/json { "expirationDateTime": ...", "nextExpectedRanges": ["26-"] }
Resuming & cancelling an upload session To resume an upload session, first determine the missing range(s) by submitting a request for the upload session: HTTP GET https://sn3302.up.1drv.com/up/fe6987415ace7X4e1eF866337 Use the response to determine missing ranges & resume uploading the missing ranges To cancel an upload session, submit an HTTP DELETE to the upload session: HTTP DELETE https://sn3302.up.1drv.com/up/fe6987415ace7X4e1eF866337
Uploading large files with the Microsoft Graph .NET SDK var fileName = "largefile.zip"; var currentFolder = System.IO.Directory.GetCurrentDirectory(); var filePath = Path.Combine(currentFolder, fileName); // load resource as a stream using (Stream fileStream = new FileStream(filePath, FileMode.Open)) { GraphServiceClient graphClient = GetAuthenticatedGraphClient(...); var uploadSession = graphClient.Me.Drive.Root .ItemWithPath(fileName).CreateUploadSession() .Request().PostAsync().Result; }
Uploading large files with the Microsoft Graph .NET SDK { ... // create upload task var maxChunkSize = 320 * 1024; var largeUploadTask = new LargeFileUploadTask<DriveItem>(uploadSession, fileStream, maxChunkSize); // create upload progress reporter IProgress<long> uploadProgress = new Progress<long>(uploadBytes => { Console.WriteLine($"Uploaded {uploadBytes} bytes of {fileStream.Length} bytes"); }); // upload file UploadResult<DriveItem> uploadResult = largeUploadTask.UploadAsync(uploadProgress).Result; if (uploadResult.UploadSucceeded) { Console.WriteLine("File uploaded to user's OneDrive root folder."); } }
Required permissions for upload files Permissions involved in working with files: Delegated permissions Files.ReadWrite Files.ReadWrite.All Sites.ReadWrite.All Application permissions Files.ReadWrite.All (for SharePoint site collections) Sites.ReadWrite.All (for SharePoint site collections)
Insights overview Agenda Trending Insights Used Insights Shared Insights
Files insights Insights are relationships calculated using advanced analytics & machine learning techniques Microsoft Graph includes the following insights APIs: Trending: documents trending around a specific user Used: documents viewed & modified by the user Shared: documents shared with a user Each insight record returned includes two objects: resourceVisualization: includes properties for displaying results (title, previewImageUrl) resourceReference: includes details on the returned record (URL, ID, file type)
Files trending around a user Obtain rich relationships showing document connections trending around a user https://graph.microsoft.com/v1.0/me/insights/trending Response: { "value": [{ "id": "id-value", "weight": "weight-value", "resourceVisualization": { "title": "title-value", "type": "type-value", "mediaType": "mediaType-value", "previewImageUrl": "previewImageUrl-value", "previewText": "previewText-value , ... }, "resourceReference": { "webUrl": "webUrl-value", "id": "id-value", "type": "type-value } }}]}
Files trending around a user (Microsoft Graph .NET SDK) GraphServiceClient graphClient = GetAuthenticatedGraphClient(...); var results = client.Me.Insights .Trending .Request() .GetAsync() .Result; foreach (var resource in results) { Console.WriteLine("(" + resource.ResourceVisualization.Type + ") - " + resource.ResourceVisualization.Title); Console.WriteLine(" Weight: " + resource.Weight); Console.WriteLine(" Id: " + resource.Id); Console.WriteLine(" ResourceId: " + resource.ResourceReference.Id); }
Listing files access / modified by user Calculated insight that includes list of documents user has accessed & modified https://graph.microsoft/com/v1.0/me/insights/used Includes documents from OneDrive and SharePoint Online sites Response: { "value": [{ "id": "id-value", "lastused": { "lastAccessedDateTime": "..", "lastModifiedDateTime": ".." } "resourceVisualization": { } "resourceReference": { } }}]}
Listing files access / modified by user (Microsoft Graph .NET SDK) GraphServiceClient graphClient = GetAuthenticatedGraphClient(...); var results = client.Me.Insights .Used.Request().GetAsync().Result; foreach (var resource in results) { Console.WriteLine("(" + resource.ResourceVisualization.Type + ") - " + resource.ResourceVisualization.Title); Console.WriteLine(" Last Accessed: " + resource.LastUsed.LastAccessedDateTime.ToString()); Console.WriteLine(" Last Modified: " + resource.LastUsed.LastModifiedDateTime.ToString()); Console.WriteLine(" Id: " + resource.Id); Console.WriteLine(" ResourceId: " + resource.ResourceReference.Id); }
Required permissions for working with files insights Permissions involved in working with files: Delegated permissions Sites.Read.All Application permissions: Sites.Read.All