Accessing Files with Microsoft Graph
Key Takeaways
Accessing files with Microsoft Graph enables developers to use a single API to work with files in OneDrive, allowing for real-time co-authoring and access to files from various contexts such as Microsoft Teams, Groups, and SharePoint.
Full Transcript
hi I'm Andrew Connell and I'm a Microsoft MVP in the area of Microsoft 365 development and I have a lot of experience with SharePoint development including all the different development models that we've had Microsoft graph development Microsoft team's development and developing add-ins for Microsoft Office as well as working with Microsoft identity including a sure ad onedrive is the files hub for office 365 people work with files in different contexts such as Microsoft teams groups SharePoint and many more places with onedrive users can access these files no matter where they are stored in Microsoft graph enables you to use a single API to work with these files files and office 365 are stored in drives users can store files in a personal drive such as their onedrive for consumer or in a shared drive powered by a SharePoint document library one drives flexibility lets users collaborate however it works best for them users can share files links to files copy or move files to team drives or even attach onedrive files to mail messages in Outlook in this section you'll learn how to get lists of individual files from onedrive with Microsoft graph Microsoft graph can be used to get a list of files or download an individual file using its unique ID or with a relative path to the file in a SharePoint site Microsoft's Office 365 developer vision focuses on the users experience and their data as a developer you can bring your applications into their user experience with over 1.2 billion users of office worldwide this is a huge opportunity to provide a window into your applications as well as being able to connect into the into their data and add intelligence to your applications there are currently 850 million events created a month and a total of 470 petabytes of data or more in the service that can add value for your users Microsoft graph is the Gateway to your data in the Microsoft cloud as you can see from the list of resources services and entities on this slide there are lots of things that you can take advantage of in the office 365 platform you can work with mail calendar contacts tasks in addition to content inside of SharePoint sites and lists files inside of onedrive and interact with Microsoft teams and many more things that you can take advantage of each of these services typically has its own API that you can leverage to read and write to these different services however it can be challenging and cumbersome to work with these different services as each is implementing their own endpoint permission model and syntax for performing some operations like searching and other subtleties each endpoint is also secured with Azure ad which means that developers will need to authenticate with Azure ad and obtain an OAuth access token for each service they want to work with the Microsoft graph addresses many of these challenges by acting as a proxy to a lot of the different services it encompasses data residing in office 365 such as mail calendar and contacts and onedrive as well as many others in addition it also provides access to Windows 10 and enterprise mobility and security under a single endpoint this endpoint HTTP colon slash slash graph Microsoft comm allows developers to create applications that will only have to authenticate once and obtain a single access token that can be used to retrieve data from multiple sources greatly simplifying the development process each service will still have their individual permissions or scopes but all of these permissions are defined in a single place under the Microsoft graph endpoint and handles the underlying calls and permission requests to each of these different endpoints now why would you integrate with onedrive file storage in the cloud it's one reason is to tap into billions of files onedrive users can access their files from any device online or offline and share files with people inside or outside of their organization onedrive enables real-time co-authoring and familiar apps like Word Excel and PowerPoint and files light up with rich thumbnails for hundreds of formats video streaming and analytics and many more things that are all powered by Microsoft graph data onedrive is protected with advanced encryption compliance and security features that these customers you can also store your apps files in a powerful cloud when you store your files in onedrive your app can take advantage of the features of Microsoft cloud and your users can access their files anywhere use the file picker SDK to quickly open download save and share files stored in onedrive from within your own app using the same experienced onedrive users are familiar with get information about selected files directly from the picker SDK or use Microsoft graphs API directly to interact more deeply with the files use specific folders to store files in well-known locations in onedrive like documents and camera roll or give your app its own personal folder you can also bring your app straight to users within onedrive onedrive consumers can use or launch your app directly from with one from within onedrive - open edit or preview files use onedrive file handler extensions to provide icons and previews for your own custom file extensions add your app to the new button or even add your own custom actions to the menu bar to launch rich to launch your app you can also work with content and formats that your app is going to understand your out can get file content in the format that is the most convenient for you your app can display custom sized thumbnails for hundreds or different file formats and you can download files in different formats like PDF doc X and others you can work with file contents and metadata without downloading the entire binary with Microsoft graph you can access rich content through REST API is without having to download the binary you can explore extracted metadata from photo audio and video files and use the Excel API to work directly with raw data stored in an Excel workbook you can even use the notes API to access the contents of one of notebooks and finally you can react to file changes because with webhooks your app can get notified when files change so that you can quickly react use the Delta API to see what changed since the last time your apps synchronized with the Microsoft cloud now let's explore the Microsoft graph files related endpoint related resources for the Drive endpoint represents a logical container of files like a document library our users one drive a drive item represents an item within a drive like a document photo video or a folder these two resources expose data in the following ways you've got properties which are going to include things such as an ID and name and expose simple values as strings numbers and boolean x' a facet such as a file or a photo expose complex values and the presence of facets like file or folder indicate behaviors and properties of a drive item in references such as children and thumbnails point to other resource collections access to the currently signed in users onedrive using the HTTP graph Microsoft comm / v10 slash me / drive endpoint is how you're going to get to the current users onedrive this endpoint will return details about the users onedrive including the date it was created last modified quota information and what type of onedrive it is if it's a onedrive for business or if it's a onedrive for consumer to view the contents of a user's onedrive use this / Drive / route endpoint this endpoints can return the root folder of the onedrive account and include a folder property that contains the number of files and folders at the root of the onedrive account to view the contents of the folder use the children in point and you can also access a specific folder if you want developers can use one of the mini Microsoft graph SDKs to access a user's onedrive account to get the currently signed in users root onedrive folder with the Microsoft graph SDK you're gonna use the following code that you see here on the slide after first obtaining an authenticated graph service client I'm just gonna call graph service client me dr dot root and then it can use the fluent api to get additional things and do additional things with the data that's coming back this is gonna give me back a collection of results that I can then enumerate through and display each of the different items Microsoft graph also enables the signed end user to access another users onedrive provided that they have been granted access to it using the slash user slash user ID /dr endpoint Microsoft graph also allows access to the user to onedrive accounts for more than just users for instance you can even access a group's onedrive account using the groups / group ID / drive endpoint and share a SharePoint sites default document library using the slash sites / site ID / drive endpoint as well now in order to perform onedrive operations you're going to need one of the following permissions the specific permission required is going to depend on the operation that you want to perform for example if you are creating editing or deleting drives or drive items one of the right permissions is going to be required some of these permissions can be granted by a user as a delegated permission while others must be granted to the app by an administrator delegated permissions are granted by users include things like file dot read files dot read dot all files dot read write and files dot read write dot all but application permissions which must be granted by administrators include files read all and files dot read right doll now the drive item resource has a property called content and that can be used to get access to the files itself with a file the contents of the actual file only the drive items within with the file property can be downloaded now the content property returns the primary stream of the file for example to download a specific file using the Microsoft graph net SDK you're going to use the code that you see here on the slide so after again getting in reference to an authenticated graph service client I'm simply going to go find a file so I'm getting a file stream and then I'm gonna jump all the way down to and download the file by saving it as a memory stream now here in the code you see that I'm referencing a result object where I'm saying go to the content property of the file and then I'm calling request get a sink and result and that's the content property that I referred to above now in this first demo what we're gonna do is we're gonna we're gonna look at a dotnet core console app that's gonna access and download files from a user's onedrive account using Microsoft graph now before we do that though we need to go create an azure ad application using the azure ad admin Center that we our application can use I'm going to jump over to the azure ad admin Center and we're gonna go to Azure Active Directory and I'm gonna go ahead and create a new app so I'm gonna come over here to a preju stration z-- and then select new registration so I'll just call this my MS Graf console test and this is gonna be just a single tenant app so right off the bat it's going to go ahead and create my new app now these things here the client ID and the tenant ID these are things that I'm going to need in my app so I'm gonna go ahead and copy these I'm gonna go back over to my console app and I have this settings file where I can paste these values in so I'm gonna go ahead and do that before we go any further as these are things that my app is going to need and we'll see how we use those in just a few minutes now the next thing I'm going to need to do is if I come back over here to my app and I'm gonna select the redirect URI z-- there is a section here about suggested redirect your eyes for public clients now we're using a console app which would fall under the desktop category which means he's got a well-known return URI that we're gonna use so I'm gonna because we're using the Microsoft authentication library I'm gonna go ahead and select this one to ensure this will work and then the other thing I'm gonna do is I'm going to come down here on the default client type whenever I use a public client I have to make sure I turn that on this is something we turn on automatically now we're gonna use the resource owner password credential we're gonna use that flow for obtaining our access token and what that's gonna do is our console app is gonna ask the user for their username and password and then the console app is gonna use that information to go login on their behalf in a production app that may not be what you want to do I would strongly recommend you take a look at the device code flow as another option for supporting authentication with your console app I'm just we're not going to focus too much on the authentication in this module we're gonna focus mostly on using on graph and onedrive and working with files primarily now after we've created our app we also need to give it some permissions so I'm going to go to the API permissions and I'm going to add a permission I'm going to select Microsoft graph and we're gonna create a delegated permission and this delegated permission we're looking for is files dot read so go ahead and add files dot read and say add the permission now once I've gone through to added this permission because we're not doing an interactive a login we're asking the user for their username and password so one of the things that I'm gonna want to do here to make my life just a little bit easier is I'm gonna go ahead and grant the permission and the reason for this is because we don't have a and interactive login we're asking the user for their username and password and we're logging on their behalf and so they don't have a way to grant this files not read permission to our app so I'm gonna do that by just going to this admin consent for contoso and I'm gonna log in as my current user and I'm going to consent on Megan's behalf now I'm logged in as Megan so it's not a big deal in this case but this is just something I want to do so it's go ahead and it's adding the admin consent right now and so we just give it a second and make sure that this finishes okay so we can see now that it's finished so let's go back to our app and let's take a look at this so let's see what our code is doing so inside of program CS what we're doing here is we are first going to load the settings for our app so this function is just gonna be loading those app settings that contains the tenant ID and the app ID from Azure ad the next thing that it's going to do is we're going to prompt the user for their username and their password after we do that we're then going to go obtain an authenticated Microsoft graft client now the way we do this let's go jump down here the definition for this the way we're doing this is we're creating an author we're creating an authorization provider you see that's being done up here that contains the configuration that we are going that we defined and that's inside of our has our app ID and our tenant ID we have the username and the user's password as well so what I'm doing is I'm going to go create a new Authority that uses the tenant ID and points to the v2 Azure ad endpoint and then also gonna add in the two scopes that we need create those in an array for a list of all the permissions we want and then I'm gonna create using the Microsoft authentication library I'm going to use this thing called a public client app builder our application builder to create a public client with this client ID using this authority and then build it this public client application is what's used for applications that are distributed to your users such as a console app or a mobile app because they don't have a password or certificate associated with them because we can't trust them once we distribute the code to our end user they could really do anything they want with it so that's why we want to make sure that we have a little bit more control over this I'm then going to create an instance of the Microsoft authentication provider passing in the client application that we created the array of permissions and the username and password this emcell authentication provider is a little helper bit of code that we've written here if we take a look at this and what this is doing is a singleton it's got--it's it's creating let's see where's done here are getting an instance of it right here is we're creating an instance of it so if it doesn't already exist we're creating an instance of it passing in these values and we're just saving them as properties on our application and then if I scroll down a little bit farther after we got our instance of it so let's go back to our code right here so once we have our instance of it you'll see how this is being used as an authentication provider so that's what's being passed back as our authentication provider that's what's being returned back to the the caller I'm creating a new instance of the graft service client that provider that I'm passing in what that's doing is is that whenever you'll see here that there's a couple known well-known methods on it whenever I this method called authenticate async requests is called or whenever get token async is called what this is going to do is it's going to know how to go get access to the currently logged in user and then try to obtain an access token on behalf of that user for these permissions using that account and handing that access token back so it's this authentication provider is handed over to the graft service client which knows how to handle all the authentication now with emcell alright so now we're in a point where we have a reference let's go back up here to our application so where we have a reference to the graft service client and then I'm gonna get a reference to all the files in the current in the root folder for the current user so I'm gonna do that by going to graph client mean dot dr dot root dot children and then issue that request and then called get a sync with the results and it's gonna write out all of the all the files and one of the things that we can do is we can see what the underlying rest request is if i uncomment this right here so let's go let's run the application and let's see what happens so we're gonna do a dotnet build and a dotnet run so after building the application asked me for my user name so I'll go ahead and paste that in is then gonna ask me for my password so I will paste that in as well and it should go list out everything that we have in our current root of our one drive so you see I went to me slash drive such root slash children and it's gonna find all the files so for example it's gonna look for a file how about the broken pipe file right here and if I go back to the browser we can see that we did have a file here called broken pipe now another thing we can do is we can get a reference to a specific file so let's go ahead and let's hide all of this and I'll come back down and we will look for a specific file here so this specific file let's go through and let's hide that request - this is the ID of a very specific file so let's go find let's update that 2.2 or how about this one the CR 27 to 27 so let's just paste that in and we'll see how we're getting a reference to a specific file when that's being written out so let's run that same application again and we will log we have to log in again so I pasted my username in I'll grab my password from off the screen and here we'll get reference to a specific file so here you can see we've got that specific file and the way I'm getting it is by using the path for the qualified me /dr such items slash and then the ID the last request that I wanted to show you is how to download a specific file so again we're gonna grab this file we'll paste in that ID and we will uncomment these lines here and we will also uncomment all these lines so what's happening now is we're going to create a stream and we're going to figure out the path of where we want to save the file I'm gonna save it right to the same directory where my application is created it's going to be called dry the item underscore with the file ID and I don't know what kind of file is so I'm just gonna say file I could do that I could grab the name of it as well but we're just gonna save a little bit of time here in this demo code and then I'll have the drive item listed out I'll go ahead and grab that the specific item that I'm looking for and then I'm just using the stream to say copy everything to that stream and save the file so again let's run this and again it's going to prompt us to login password and here you can see that once it is saved the file you see it's listing the file out that it actually saved and here's the file that we just downloaded right here using the same ID so in this demo you saw how to get a list of all the files for the current user how to get a file a specific file using the files ID and then finally you also saw how to download a file again if you want to see this code in action you want to build this yourself there is a lab associated lab exercise associated with the module that the screencast is associated with that walks you through all the steps including creating the azure ad app registration using the azure ad admin center and writing all the code as a dotnet core console app and working with the Microsoft graph net SDK to download the file in this section you'll learn how to upload files to onedrive using Microsoft graph including both small and large files now Microsoft graph supports uploading files up to four megabytes in size using a simple HTTP put request to upload the file you simply are gonna submit an HTTP put to the collection where the new file should be created the end of the endpoint must include the name of the file to create and the end with the content endpoint as well so the request must include the HTTP request header content type with a value that matches the type of the file that's being uploaded in this case here is a simple text file the contents of the file should be included in the body of the request for example to upload a text file to the currently signed in users root onedrive account I'm going to use the following HTTP request so notice here I'm pointing to the me slash drive slash root end point and then specifying the file and then after I specify the file name I'm then putting slash content notice the content type is text slash plain and then there's an empty line or a blank new line and then I've got the contents the file that I'm uploading to update the file or to replace an existing file simply do a put to the still of the existing file using the files ID and so notice here I'm going to slash Drive slash items and putting in the item ID with the slash content on the end point you can also use a Microsoft graph net SDK to perform the same action assuming a file named my new small file text is present in the current folder I'm going to use on my local machine where the app is going to run I'm gonna use the following code to upload the file to the currently signed in users root onedrive account so I'm getting a reference to the file name in Korean and the entire path to that local file I'm getting a file stream to that file and then after getting an authenticated graph client I'm gonna go to the me drive root endpoint I'm gonna point to an item using a specific path and then go to that file content create a request and then call a put a sync saying I'm gonna be uploading a drive item and then passing in the file stream that I want to upload now what about uploading larger files that are 4 Meg's greater than 4 Meg's so Microsoft graph also supports uploading files larger than 4 Meg's up to the maximum file size that's supported by onedrive the support for large files also enables developers to resume a transfer in scenarios where the upload was either interrupted or paused uploading a file involves creating a upload session followed by uploading bytes within that session and as the upload session uploads the file it reports back on the progress allowing developers to provide feedback to the user that is uploading the file so to upload a large file I'm gonna start by creating a new upload session so you see here we're going to me slash drive slash root and then slash create an upload session this is going to return the upload session that contains the URL to upload the file and whether when the temporary upload location is going to expire expire so here it's saying that the file is gonna be called large file dot zip now as long as the expiration date has not passed you can resume the upload session when it is interrupted or it's been paused so you can see here I'm uploading the file large file zip that's this I'm gonna create where I wanted to go and then I'm getting a response at the bottom that tells me the location of where I should be uploading it and then second is going to include the expiration date and time now once you've uploaded the session or once you've created the upload session you can then upload bytes to the temporary file in onedrive now in the example you see here I'm uploading the first 25 bytes in a hundred and twenty eight bit file and you can see that when you look at the first code reference there that you see the content length that I'm uploading is 26 and the byte range is 0 to 25 out of 128 and then I'm going to stream the first 25 bytes of the file the content range is then going to specify which chunk that I'm uploading because that's the thing we just went through and the total size of the file the total size the file should not change between the upload sessions if the total size the file changes between upload sessions the upload session will fail the previous request will yield the following response that you see at the bottom of the slide here that will again state the expiration time and the next expected range for the file so it's expecting 26 that bytes 26 through something when the last range of files are see by the upload session it'll return in HTTP 201 created or an HTTP 200 ok and with response and it will include the drive item that represents the currently created file now upload sessions can be resumed in scenarios where the session was interrupted or paused and to resume and upload session you first determine the missing range by submitting an HTTP request a get request to the upload session and that's gonna return the same type of response that was returned when a range is uploaded use the response to determine the next part of the file that you should upload now the cancel and upload session simply submit an HTTP delete request to the upload session endpoint as follows as you see there at the bottom of the slide the following code is going to upload a large file to the currently signed in users onedrive root folder using the mics Groff net SDK so here at the top I'm getting a reference to the file path fully qualified path for a file and then once I have that path I'm then gonna go upload the file I'm gonna do that by saying graph client that mean drive route I'm gonna create the item and then I'm gonna create a new upload session and then say request dot post async dot result and here's more of the code that you see here what I'm doing is I'm creating the upload task so that was before now what you see here is I'm figuring out what the maximum size the file is that I'm going to upload so I'm creating a large file upload task by passing in the session passing in the file stream in the maximum chunk size I'm then going to create an upload progress reporter this is one of the cool things that Microsoft graph net SDK provides us when I when I create this progress reporter what this is is a function that's going to write out something to the console every time we get a response back for a new chunk that we've uploaded and then finally I'm going to upload the file by saying large upload task upload async passing in my progress reporter and then calling the dot result and what that will do is once the it'll go ahead and start uploading the file and once the upload is succeeded it will notify me saying the file has been uploaded now in order to upload files to onedrive you're gonna need one of the following permissions the specific permission required depends on the operation that you want to perform so the first three are delegated permissions files dot read write files dot read write at all and sites dot read/write dot all that last one is applicable to SharePoint sites and then you can also have application permissions that are that are not that a user can't grant or a regular user can't grant but an administrator will have to grant in this exercise you're gonna learn how to upload a file to onedrive using the Microsoft graph specifically we're going to see how to upload both small files those smaller than 4 Meg's and large files so we have in our code that you see here we have in our project I have a small file dot txt which is just a just a file I just found on my machine which is looks like peer as a just a Microsoft sequel statement and then at this large file is a zip file from a theme and it's over a hundred Meg's that were it's over a hundred Meg's so this was just a few K this was over a hundred Meg's so let's take a look at the code here well Before we jump into the code we need to go make a change to our app registration in Azure ad and the reason why we need to do this is because we need to add a new permission to our app and specifically we need to give our app a delegated permission of files dot readwrite so we'll do files dot read/write we'll grant this permission and then after granting it we need to go through and or adding the permission we then need to have our user grant it and again just like the last demo I'm gonna use admin consent to make this a little bit easier for our login experience in the demo not something I would potentially recommend if it was a production app but it'll work just fine for our use so let's go look at the code for this and what code changes to be make well the main code change one of the main code changes that I made that's important is I had to go add in an additional permission in the array of scopes when our application runs so it's gonna have to also ask for an access token for files dot read/write now the next thing I did is if you come over here and look at our code we made a little bit of a change to our code right in this area so what I'm doing is I'm getting a reference to our small file and that's just the local file on our local and our local project and then I'm creating opening up a stream to that file and then I'm gonna go upload that file by saying client mean dr dot root i'm gonna tell it where i want to create the file or what the name is i want to create i'm going to tell go to the content endpoint for the file submit a request that part of the request to create a request in as an HTTP put and pass in the file strain and that will go upload our file so let's see this in action so now we're going to run a dotnet build and a dotnet run and that's gonna download all the dependencies from my project from NuGet it's gonna then run the project prompt me to log in just as you saw in our last example I'm having to go login and pass in my username and then my password we can see it's uploading this small file here and it tells us where the file was created which we should see in our onedrive there it is so we can see it was this small file was created just a few seconds ago which is correct that's what we just uploaded and you can see it's a very small file now the other thing I want to do is I want to upload a large file so what I'm going to do is let's hide all of this for a second and let's comment all this code out and let's uncomment let's see uncomment the reference to the large file and then we're gonna upload it as a stream so I need to then uncomment all of this as well and what this is gonna do is create a new stream by opening up that exit but the difference here from the last sample is we're creating an upload session and I'm calling post async now notice what we're doing here I'm creating an upload task and so this to upload tasks that I'm creating right here I'm gonna create that and specify here's this session that we're creating which is what we get back the stream which is the file that we're gonna be uploading and then this the chunks that we're gonna be uploading I then create a progress object and the progress object is what I'm going to be able to use when I call the upload task upload async and every time I get a response back from Microsoft graph as it progresses through uploading the file it will then turn around and write out the status of the file that is being uploaded so let's see this in action so I'm gonna come over here and let's say dotnet build and run just like we did last time I will log in and pass in my user account username and password and it will start uploading the file now this file is about hundred 150 Meg's something like that you can see the progress is actually running so every time it's reporting back a chunk that's uploading it's gonna run that progress so you can see it's uploading everything it's almost finished there about five hundred out of the five sixty four still going Oh actually you know it wasn't I was off by a digit so it's gonna take a second for this to run and what we'll do is see the magic of video editing I'm gonna speed this up so that you don't have to watch this here we can see that the file has now been completely uploaded let's go double check that we'll go back over to our browser and here again we can see the large file has now been uploaded it is 53 Meg's in size and you can see that it was just uploaded just a few seconds ago so in this demo you saw how we uploaded files using the Microsoft graph SDK we did it both using the uploading a small file and a large file Microsoft graph enables more features than just reading and writing files and in this unit you're gonna learn how to generate a list of files that are trending around a user and how to get all the files owned by a specific user insights our relationships calculated using advanced analytics and machine learning techniques using Microsoft graph you can identify onedrive documents that are trending around specific users insights or relationships again as I said that are calculated using advanced analytics and machine learning techniques and using Microsoft onedrive we can identify onedrive documents that are trending around users insights are returned by the following api's there's a trending API and what that does is return documents from onedrive and from SharePoint sites trending around a user the used endpoint returns documents viewed and modified by a user and includes documents that are you that the user used in onedrive for business SharePoint and opened as email attachments as well as and as a link to an attachment from sources like box Dropbox and onedrive the shared endpoint returned documents shared with a user and documents can be shared as email attachments or as onedrive for business links sent in emails each insight is returned with a resource visualization and a resource reference complex value type the resource visualization contains properties such as the title the in the preview image that can be used in the UI so this is a request that's gonna return a collection of documents each with an assigned weight that can be used to rank their relevance so you can see here we're getting an ID value back that's in a weight value and the ID is the ID of the specific insight none of the the actual object itself but the insight reference the weight is how things are weighted you can use that to sort things either higher weight which would be preferred in most scenarios or you can sort them in reverse order and then we have the resource visualization and the resource reference objects again the resource reference objects those will be used in a nice web UI or a nice UI experience to give some preview of the data that's being shown so you have the web URL value and the type and those can be things like thumbnails and such and then you also have or those how you're gonna go find the actual resource the visualization is what can be used in the preview rendering so you have like the title of whatever the document is that the type of the document the media type of the document so is in an image is it a PowerPoint slide presentation is it a word doc and then there's a preview image that you can use that'll be of a URL to that preview image and then finally there's preview text as well if that if that's applicable you can also obtain the trending files around the currently signed in user with the Microsoft graph net SDK and this code shows you how to do that as you see here the only thing you're really doing here is you're just at you're going to the me end point dot insights dot trending and then walking through each one of these different results that comes back I'm simply gonna write out some information about the resource visualization the weight and the resource reference as well now calculated insights that include a list of documents the user has modified this insight returns the most relevant documents that a user has viewed or accessed the documents returned include those found in onedrive for business and a SharePoint site this is the the used calculated in site to get a list of the documents that are recently modified or accessed by the current user you're gonna submit the following request so you're gonna submit a quest to slash me slash incites slash used um this request is gonna return a similar response to what we saw with the trending endpoint except that each item returned doesn't include a weight and in the collection it's gonna include a last used object with two properties when it was last used or accessed and when it was also last modified you can do the same thing with the dotnet SDK as well here instead of going to the dot insights dot trending I'm going to the dot insights dot used endpoint and again I can just write the results out to the console as you see here and in order to access insights on files in onedrive you're going to need one of the following permissions the specific permission required depends on the operation that you want to perform so you have delegated permissions or really just one which is sites not read at all and application permissions which again can also be sites dot read dot all in this last demo we're gonna see how Microsoft graph enables more features than just reading and writing files and in this exercise you're gonna learn how you can get a list of files that are trending around a user and how to get the recently modified and accessed files by the current user and the first thing we need to do is we need to go give our apps some new permissions so I'm gonna jump back over to my azure ad admin center and add a new delegated permission and this is going to be pointing to these sites sites dot read dot all we will add that permission and just like I did a couple of moments ago and the last demo you're gonna we're gonna go ahead and grant admin consent for it or use admin consent to grant this permission all right now that the the permission has been granted let's go back to our console app and let's take a look at some of the changes that we made so again like like I showed you last time one thing we have to do is we have to add the scope the permission that our app is going to need so I'm doing that by adding in an additional scope now the next thing we're gonna do here is we're gonna let's take a look at these requests that we're going to make so let's look at the first request first because it makes sense to do it in that order here we're gonna get a list of all the files that are trending around the current user and so in this case here I'm going to the insights dot training endpoint to get a reference to all the files that are trending around the current user and I'll write out the visualizations and details about the resource so in order to run this we have to open up the console again and do a dotnet build and a dotnet Rock so I'll pay stem my username for my test user and their password and let's see what we get back all right so we get a little bit information back we can see that there's an excel file called marketing initiatives and you can see we can tell what type of a file it is so this is an excel dr. word doc we're also getting the weight of this file so let's see if we have there's the ID oh we're actually getting the second one here let's try it let's do that one more time we're we we commented out request to but I forgot to save the file so we're getting request one so actually let's just do this in reverse order here so this is what we were doing just this is what we just ran a second ago we just didn't save it so this first endpoint that we're hitting is the used endpoint and this is telling us when the file was last access and last modified and because we just uploaded those two files a minute ago or because we have these other files here you can see that it's giving us the files that were most recently accessed around the current user the insights API is not immediate there's a little bit of a delay and so even though we just uploaded some files a moment or two ago in the demo they're just not they're not showing up just yet all right so that's the use resource we can also do the same thing or do a similar thing by looking at files that are trending around a particular user so let's uncomment those this time we will save it and when I rerun our project and login this time we will see files that are current that our user has is that are trending around the current user alright so here we a little bit different so you can see we're getting a wait back for this annual report the ID of the file or the ID of the the insights object and the resource where that file is we can scroll up a little bit and let's see what other files it's finding for us so we can see that this has got the heaviest weight here of for all the files or returns I think it thinks that this one is the best the best match for that is our files that are trending around the current user and so here I showed you how we can use the Microsoft graph net SDK to get some insights around the specific files I want to thank you for taking the time to watch this module or watch this screencast on this module this screencast is associated with a Microsoft learning module that contains hands-on lab and demos of all of the demos that you saw in this screencast if you want to take the time to do the demos on your own it'll walk you through each one of them as well as references to pre-built samples showing you exactly what we did in these modules so with that thank you very much I hope you learned something about working with files and Microsoft graph
Original Description
OneDrive is the files hub for Office 365. OneDrive enables users to access and collaborate on files no matter where they're stored. Microsoft Graph enables developers to use a single API to work with the files in OneDrive. Files in Office 365 are stored in drives. Users can store files in a personal drive, their OneDrive, or in a shared drive powered by a SharePoint document library. In this module, you’ll learn how to access files in OneDrive, both reading and writing files, and how to traverse relationships between files and users.
At the end of this module, you should be able to:
Demonstrate how to retrieve a list of files OneDrive
Demonstrate to download a file from OneDrive using a unique ID
Get a list of files trending around the signed in user
Demonstrate how to upload a large file to OneDrive
For more on this module visit: https://docs.microsoft.com/en-us/learn/modules/msgraph-access-file-data/
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Microsoft 365 Developer · Microsoft 365 Developer · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Adaptive Cards community call-February 2019
Microsoft 365 Developer
PowerApps community call-February 2019
Microsoft 365 Developer
Microsoft Graph community call-March 2019
Microsoft 365 Developer
Office Add ins community call-March 2019
Microsoft 365 Developer
PowerApps community call-March 2019
Microsoft 365 Developer
Microsoft Teams community call-March 2019
Microsoft 365 Developer
Using React and Office UI Fabric React Components
Microsoft 365 Developer
Build Microsoft Teams customization using SharePoint Framework
Microsoft 365 Developer
Microsoft Graph community call-April 2019
Microsoft 365 Developer
Using Change Notifications and Track Changes with Microsoft Graph
Microsoft 365 Developer
Office Add Ins community call-April 2019
Microsoft 365 Developer
Adaptive Cards community call-April 2019
Microsoft 365 Developer
Microsoft Teams community call-April 2019
Microsoft 365 Developer
Getting Started with Microsoft Graph and Application Registration
Microsoft 365 Developer
Getting Started with Microsoft Graph and the Directory API
Microsoft 365 Developer
Getting Started with Microsoft Graph and Microsoft Teams
Microsoft 365 Developer
Getting Started with Microsoft Graph Explorer
Microsoft 365 Developer
Getting Started with Microsoft Graph
Microsoft 365 Developer
Getting Started with Microsoft Graph and Mail API
Microsoft 365 Developer
Getting Started with Microsoft Graph and Office 365 Groups
Microsoft 365 Developer
Getting Started with Microsoft Graph and the Calendar API
Microsoft 365 Developer
Getting Started with the Microsoft Graph Toolkit
Microsoft 365 Developer
Getting Started with Microsoft Graph and JavaScript SDKs
Microsoft 365 Developer
Getting Started with Microsoft Graph and .NET SDKs
Microsoft 365 Developer
Discover how businesses can be more productive with Microsoft 365 integrations
Microsoft 365 Developer
Adaptive Cards community call-May 2019
Microsoft 365 Developer
Office Add-ins community call-May 2019
Microsoft 365 Developer
Why We Built on Microsoft Teams
Microsoft 365 Developer
Microsoft Teams community call-May 2019
Microsoft 365 Developer
Microsoft Graph community call-June 2019
Microsoft 365 Developer
Build Angular SPA's with Microsoft Graph - June 2019
Microsoft 365 Developer
Office Add -ins community call-June 2019
Microsoft 365 Developer
Build Android native apps with the Microsoft Graph Android SDK - June 2019
Microsoft 365 Developer
Build MVC apps with Microsoft Graph - June 2019
Microsoft 365 Developer
Authenticate and connect with Microsoft Graph - June 2019
Microsoft 365 Developer
Microsoft Graph data connect - June 2019
Microsoft 365 Developer
Change notifications with Microsoft Graph - June 2019
Microsoft 365 Developer
Build iOS native apps with the Microsoft Graph REST API - June 2019
Microsoft 365 Developer
Build Node.js Express apps with Microsoft Graph - June 2019
Microsoft 365 Developer
Smart UI with Microsoft Graph - June 2019
Microsoft 365 Developer
Leveraging the Microsoft Graph API from the SharePoint Framework - June 2019
Microsoft 365 Developer
Build UWP apps with Microsoft Graph - June 2019
Microsoft 365 Developer
Build React SPA's with Microsoft Graph - June 2019
Microsoft 365 Developer
Getting Started with Microsoft Graph and Batching
Microsoft 365 Developer
Getting Started with Microsoft Graph and Change Notifications
Microsoft 365 Developer
Getting Started with Microsoft Graph and Consent Permissions
Microsoft 365 Developer
Getting Started with Microsoft Graph and Education
Microsoft 365 Developer
Getting Started with Microsoft Graph and Financials
Microsoft 365 Developer
Getting Started with Microsoft Graph and Excel
Microsoft 365 Developer
Getting Started with Microsoft Graph and Data Connect
Microsoft 365 Developer
Getting Started with Microsoft Graph and Intune
Microsoft 365 Developer
Getting Started with Microsoft Graph and Notifications
Microsoft 365 Developer
Getting Started with Microsoft Graph and OneNote
Microsoft 365 Developer
Getting Started with Microsoft Graph and OneDrive
Microsoft 365 Developer
Getting Started with Microsoft Graph and Open Extensions
Microsoft 365 Developer
Getting Started with Microsoft Graph and Paging
Microsoft 365 Developer
Getting Started with Microsoft Graph and Schema Extensions
Microsoft 365 Developer
Getting Started with Microsoft Graph and Security API
Microsoft 365 Developer
Getting Started with Microsoft Graph and Query Parameters
Microsoft 365 Developer
Getting Started with Microsoft Graph and Reporting API
Microsoft 365 Developer
More on: Tool Use & Function Calling
View skill →Related Reads
📰
📰
📰
📰
The Future of Artificial Intelligence in 2026: How AI Is Transforming Every Industry
Medium · Startup
Samsung's Record Profit Overshadowed by AI Chip Demand
Dev.to AI
The Real Reason 99% of People Will Fail to Make Money with AI in 2026
Medium · AI
Introducing AIRI and the Blueprint for Sovereign Intelligence
Medium · AI
🎓
Tutor Explanation
DeepCamp AI