Office 365 iOS SDK Swift Sample -- Create, Read, Update and Delete Files

Here are some code snippets to show you how to create, read, update and delete files (i.e. the CRUD operations) from OneDrive for Business/SharePoint using the Office 365 SDK for iOS. The underlying implementation is a REST-based API, but we don't have to worry about this here - we can just use the abstract objects created for us in the SDK.

For a detailed step-by-step explanation of how to set up a project that uses the Office 365 Objective-C SDK in Swift, check out this other article I wrote.

Before doing anything: Create an MSSharePointClient

The parameter for url is your SharePoint URL. To figure that out, go to and click on OneDrive. Copy the front part of that address on the page and put /_api/v1.0/me after it. You can also use Discovery Service to figure out what your OneDrive for Business API endpoint is.

In order to get the access token, you either use the Azure Active Directory Authentication Library (ADAL) [see how to use it here] or you can hard-code one in (NOT recommended; use only for debug & prototyping purposes).

Get Files: Let's get all the files in our root folder.

Line 6 is assuming you are populating a table with the data - you'll always want to reload the table. (Scroll to the end of this post for details on the sample I created, that uses a table) 

To iterate over the files and get their properties, you can do something like:

Use intellisense to figure out what are the properties a file has.

Get Content of a Single File: Let's say you essentially want to download a file and read its contents. How do you do that? See the code sample below:

Line 4 prints out the byte array, while Line 5 converts it into a string first before printing it.

Create File: The following code snippets show how to create a file. There are two calls you have to make, addItem and putContent.

Update File: Now, let's learn how to update an already existing file. This is pretty much a call to putContent, the same as the putContent part of creating a file.

Delete File: Finally, let's see how we can delete a file. 

Line 1 of the code snippet above contains a tableview specific property, indexPath.row. Also, please note that you need to add a custom header with the name "If-Match" as shown above. As quoted from the official MSDN documentation library,

The update and delete APIs require If-Match HTTP header to be provided with eTag of the resource in order to confirm that the client is modifying the resource from previously known state. A generic value of * could be provided to override the check. However it is not advised to override this option.

Since we are doing all this in context of a table, let's see what the full sample app looks like in Swift.

Note: This is a super bare-bones app that I made within an hour, so I'm not going to try to pretend that it contains the best UI or Swift coding practices. But it does get the job done -- it shows you how to use the Office 365 iOS SDK in Swift.

Get the sample code here from my GitHub repository.

Here's what the FilesTableViewController looks like:

When the app is run, I am first prompted to sign in. Note: This sign in screen only shows up the first time.

After I do so, I click on "Files". This brings me to a table with all my files listed:

Pull to refresh works in this view, so drag down and release to refresh and get the files from the server again. The corresponding method in the FilesViewController is the getFiles method.

Tapping on "+" brings up an AddFileController, and allows you to type in a title for the file and edit its contents (by default, it's some Lorem Ipsum text). Tap upload to upload the file as shown in the screenshot below:

Back at the screen that has the list of all files, tap on a row in the table to view more details of the file. This is the "Read" part of the CRUD operations to read the file contents & properties. As the image below shows, you can see the datetime last modified, as well as the title of the file in the navigation bar.

You'll also see the contents of the file in an editable text view, that you can tap on to edit. After editing, press Update File to save the changes to the server.

Sliding left on a cell shows a Delete button, and it deletes the actual file from the OneDrive for Business cloud storage.

Check out the methods in FilesViewController to see all these in action. Once again, here's the link to the GitHub repo with the full working sample. I hope this quick guide has been useful to you - leave your questions in the Comments section below.