Move, copy, create, or delete a file or folder on OneDrive (iOS)

You can move, copy, create, or delete a user's files or folders on Microsoft OneDrive, from your iOS apps.

In this article
Prerequisites
Move or copy a file or folder
Delete a file
Create a folder
Delete a folder
Remarks

Prerequisites

We assume that the user has already signed in and consented to the wl.skydrive scope for reading file or folder properties. If you have not added user sign-in to your iOS apps, see Signing users in (iOS).

We assume that you are familiar with Objective-C, and can write iOS apps.

Note

You can't create files directly on OneDrive. Instead, you upload files to OneDrive. To upload files, see Downloading and uploading files (iOS).

Move or copy a file or folder

To move or copy a file or folder, use code like this. The wl.skydrive_update scope is required. Change the file ID to the file ID or folder ID of the file or folder that you want to move or copy. Change the folder ID to the folder ID of the target folder to which you want to move or copy the source file or folder.

Note

For move operations, the source can be anything except the root, and the destination must be the folder to which the item will be moved.

Note

The destination of a move or copy operation must be a folder. Folders themselves cannot be copied. Also, OneDrive storage is structured so that move and copy operations cannot occur between the OneDrive folder hierarchies of different users. For example, even if user A can read user B's files, user A cannot copy or move them to his or her own OneDrive folders.

In this example, the moveFromPath:toDestination:delegate:userState method is used to move the file. The liveOperationSucceeded method determines whether call was successful, and if so, displays a confirmation message.

To copy a file, replace moveFromPath:toDestination:delegate:userState with copyFromPath:toDestination:delegate:userState.

- (void) moveFile
{
    NSString *folderId = @"folder.2e82e8a5445fe036";
    NSString *fileId = @"file.445fe0362e82e8a5";
    [self.liveClient moveFromPath:fileId 
                    toDestination:folderId 
                         delegate:self 
                        userState:@"move a file"];
}

- (void) liveOperationSucceeded:(LiveOperation *)operation
{
    if ([operation.userState isEqual:@"get folders"]) {
        self.statusLabel.text = @"OneDrive folders are loaded.";
    }
    if ([operation.userState isEqual:@"get folder"]) {
    };
    if ([operation.userState isEqual:@"move a file"]) {
        self.statusLabel.text = [NSString stringWithFormat: @"The file (id: %@) was moved successfully.", operation.path];
    };
}

Delete a file

To remove a file, photo, video, or audio, use code like this. The wl.skydrive_update scope is required. Change the file ID to a different file ID, photo ID, video ID, or audio ID to remove a different file, photo, video, or audio.

-(void) deleteFile
{
    [self.liveClient deleteWithPath:@"file.95d463b9efcca0e1.95D463B9EFCCA0E1!113"
                           delegate:self];    
}

- (void) liveOperationSucceeded:(LiveOperation *)operation
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"API call successful!" 
                                                    message:@"File deleted successfully"
                                                   delegate:self 
                                          cancelButtonTitle:@"Ok" 
                                          otherButtonTitles:nil, 
                          nil];
    [alert show];
    [alert release];
}

Create a folder

To add a new folder, use code like this. The wl.skydrive_update scope is required. Change me/skydrive to the folder ID (or, in certain cases, the friendly name, like "My Pictures") of the folder in which the new folder will be created.

-(void) createFolder
{
    NSDictionary * newFolder = [[NSDictionary dictionaryWithObjectsAndKeys:
                                 @"Vacation Pics",@"name",
                                 @"Family vacation",@"description",
                                 @"folder",@"type",
                                 nil] retain];
    [self.liveClient postWithPath:@"me/skydrive" 
                         dictBody:newFolder
                         delegate:self];
    [newFolder release];
}

- (void) liveOperationSucceeded:(LiveOperation *)operation
{
    NSString *folderId = [operation.result objectForKey:@"id"];
    NSString *folderName = [operation.result objectForKey:@"name"];
    NSString *folderDescription = [operation.result objectForKey:@"description"];
    NSString *folderLink = [operation.result objectForKey:@"link"];
    NSString *folderType = [operation.result objectForKey:@"type"];
    self.displayLabel.text = [NSString stringWithFormat:
                              @"Folder Properties:\n"
                              @"\tId. %@\n"
                              @"\tName. %@\n"
                              @"\tDescription. %@\n"
                              @"\tLink. %@\n"
                              @"\tType. %@\n"
                              , folderId, 
                              folderName, 
                              folderDescription, 
                              folderLink,
                              folderType];
}

Delete a folder

To remove a folder, use code like this. The wl.skydrive_update scope is required.

-(void) deleteFolder
{
    [self.liveClient deleteWithPath:@"folder.95d463b9efcca0e1.95D463B9EFCCA0E1!108" 
                           delegate:self];
}

- (void) liveOperationSucceeded:(LiveOperation *)operation
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"API call successful!" 
                                                    message:@"Folder deleted successfully"
                                                   delegate:self 
                                          cancelButtonTitle:@"Ok" 
                                          otherButtonTitles:nil, 
                          nil];
    [alert show];
    [alert release];
}

Remarks

For details about the minimum required and optional structures that your app must provide to use POST, see the Folder object.