in Office 365 API

Office 365 API My Files CRUD Sample Code

Related Posts:

Following yesterday’s blog post – Office 365 Contacts CRUD Sample Code – I just published the sample code to perform the basic create, read, update and delete operations on My Files API.

You can clone the GitHub repository here.

Feel free to share the love and contribute to the code.

NOTE: I have not handled Paging yet. I will update the code once I include paging.

Lets quickly look at the code:

Read All Files:

Read All Files from a Folder:

Read File Contents:

Create a new File with File Content:

Update a File Entity:

Update File Content:

Delete a File:

Write a Comment



  1. Great to see the “MyFiles” CRUD published….I am little confused with the “Create” method. Do we need to pass null for the “Stream streamFileContent” parameter? How do we specify the FileType i.e., word/excel/powerpoint, etc….?

    • Your application would have read the desired file and converted to Stream which then can be passed along in the AddAsync method.

      You do not specify the file type. This is a file storage and you can upload any type of file. It will be up to the developer to understand the file types involved.

    • I am not sure how that would work out very well as there might be a problem with how many files you can transfer.

      Also given that you would want to know the state of the transfer of each file, I am not sure whether a method to accept multiple files would work out.

      You can however today add the files one by one and since they are asynchronous, you can track the status.

        • I am not sure about that. But did you check Open XML SDK? – I would prefer that if you are looking to manipulate Office documents. I would also recommend to use the available clients to open Office documents as opposed to trying to create your own viewer.

        • Hi Venkat,
          If you want to evaluate an HTML5 viewer which supports most file formats, ( no client side plugings ) check out PCC v9. It has a RESTful API, runs as a service and is deployed in many commercial products as a customizable, responsive design viewer, enabling mobile, as well. Best of luck!

  2. Hi Chaks,
    a nice overview, thanks for sharing. I have question concerning file upload into folders (and eventually creating sub-folders). I managed to create a folder in the root directory (Documents) via
    However, I failed to create a sub-folder.
    _spFilesClient.Files.AddAsync(strFileName, true, streamFileContent);
    was successful, but how to load a file into a (sub-) folder?
    Is/will be there interface/s which allow to specify sub-paths?

  3. I am trying to publish to an Azure website that is connected to Azure AD to pull in a user’s file. When I try to authenticate I get “AADSTS50001: https://localhost:44307/O365APIURSAITO does not have administrator consent.” when doing my test. When I test on the Azure site it says there were issues with the token. Do I need to be a global administrator in Office 365 to connect my app to that azure ad?

    • I was able to over come this by getting a global admin to save the permissions in Azure. The odd thing is I was able to do the exact same thing (Azure Directory delegate permissions allow user sign on) and save it but it did not work. When the global admin account did this the application then worked. Is this a bug or did I miss something?

      • Hi Brock, I’m having the exact same issue. can you clarify exactly what permission the administrator had to set and where to find it?


  4. Hi Chaks,

    I am using the GetFileContent method in the sample given. The downloadAsync method takes lot of time.

    var streamFileContent = await file.DownloadAsync();

    Is that the correct behavior ? Does it take more time to download a file from sharepoint. I am actually trying to sync the files in Sharepoint to my web application, in this process, I am reading all the files in sharepoint, and copying all the files to my web app. Just to copy 2 to 3 files it takes almost 10 minutes, which is too long.

  5. Hi Chaks,
    I am using the var streamFileContent = await file.DownloadAsync(); statement, but when I tried to download large file (20-30MB) , th etask is cancelled.
    Iit is probably th eHTTP request timeout and I tired to set the
    SharePointClient client;
    client.Context.Timeout and client. Files.Context.Timeout but no solution.

    Could you give me an advise how to increase the downloading time, please.

    Thanks Attila

  6. Hi Chaks,
    I have few questions, I can’t find them anywhere.
    How can I change custom property of a file (added by create column)?
    How can I set permissions for my uploaded files?


  7. Hi Chaks,
    I am trying to get all files from a folder in Office 365 API – My Files.
    If I try to use
    var filesResults = await _client.Files.ExecuteAsync(),
    I get the files from the library.
    However, I created a folder named F1 in default Documents library and tried to fetch files from it using:
    var filesResults = await _client.Files[“/Documents/F1”].ToFolder().Children.ExecuteAsync();
    Here I get NullReference Exception. I have tried several combinations like “/F1”, “/Documents/F1/”. Nothing seems to work.
    Moreover, if I create another document library say “Docs”. I am not able to get files from this as well.

    • You can use the GetByPathASync to get a folder id (or file id).
      var folderItem = await client.Files.GetByPathAsync(DirName);
      to resolve the ID for a path and use it in the request.


  • Office 365 API Contacts CRUD Sample Code – Chaks Corner March 31, 2015

    […] Office 365 API My Files CRUD Sample Code […]