File and folder properties on OneDrive (iOS)
Your iOS apps can use the Live SDK to read user's Microsoft OneDrive folder and file properties.
In this article
Prerequisites
Read a file's properties
Update a file's properties
Read a folder's properties
Update a folder's properties
Get links to files and folders
Summary
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
In this context, a file represents a file of any supported format, including a photo, video, or audio. A folder can contain child folders and files.
Read a file's properties
To get info about a file, photo, video, or audio, use code like this. The wl.skydrive scope is required.
Note
In all examples, change folderID or fileID with a different folder or file ID, respectively.
-(void) getFileProperties
{
[self.liveClient getWithPath:@"file.95d463b9efcca0e1.95D463B9EFCCA0E1!113"
delegate:self];
}
- (void) liveOperationSucceeded:(LiveOperation *)operation
{
NSString *fileId = [operation.result objectForKey:@"id"];
NSString *fileName = [operation.result objectForKey:@"name"];
NSString *fileDescription = [operation.result objectForKey:@"description"];
NSString *fileSize = [operation.result objectForKey:@"size"];
self.displayLabel.text = [NSString stringWithFormat:
@"File Properties:\n"
@"\tId. %@\n"
@"\tName. %@\n"
@"\tDescription. %@\n"
@"\tSize. %@\n"
, fileId,
fileName,
fileDescription,
fileSize];
}
Update a file's properties
To change info about a file, photo, video, or audio, use code like this. The wl.skydrive_update scope is required.
-(void) updateFileProperty
{
NSDictionary * updatedFile = [[NSDictionary dictionaryWithObjectsAndKeys:
@"Having fun at the beach",@"description",
nil] retain];
[self.liveClient putWithPath:@"file.95d463b9efcca0e1.95D463B9EFCCA0E1!113"
dictBody:updatedFile
delegate:self];
[updatedFile release];
}
- (void) liveOperationSucceeded:(LiveOperation *)operation
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"API call successful!"
message:@"File updated successfully"
delegate:self
cancelButtonTitle:@"Ok"
otherButtonTitles:nil,
nil];
[alert show];
[alert release];
}
Read a folder's properties
To read a folder's properties, use code like this. In this example, the getWithPath:delegate:userState method is used to get the folder's properties. The liveOperationSucceeded method and liveOperationFailed:operation method determine whether the call was successful, and if so, display some of the folder's properties.
- (void) getFolderInfo
{
NSString *folderId = @"folder.2e82e8a5445fe036";
[self.liveClient getWithPath:folderId
delegate:self
userState:@"get folder"];
}
- (void) liveOperationSucceeded:(LiveOperation *)operation
{
if ([operation.userState isEqual:@"get folder"]) {
self.folderIdLabel.text = [operation.result objectForKey:@"id"];
self.folderNameLabel.text = [operation.result objectForKey:@"name"];
self.folderUploadPathLabel.text = [operation.result objectForKey:@"upload_location"];
self.folderLinkLabel.text = [operation.result objectForKey:@"link"];
self.folderTypeLabel.text = [operation.result objectForKey:@"type"];
};
}
- (void) liveOperationFailed:(NSError *)error
operation:(LiveOperation *)operation
{
if ([operation.userState isEqual:@"get folder"]) {
self.statusLabel.text = @"The request to read the OneDrive folder information failed.";
};
}
Update a folder's properties
To change info for an existing folder, use code like this. The wl.skydrive_update scope is required.
-(void) updateFolder
{
NSDictionary * updatedFolder = [[NSDictionary dictionaryWithObjectsAndKeys:
@"Family vacation in the snow",@"description",
nil] retain];
[self.liveClient putWithPath:@"folder.95d463b9efcca0e1.95D463B9EFCCA0E1!112"
dictBody:updatedFolder
delegate:self];
[updatedFolder release];
}
- (void) liveOperationSucceeded:(LiveOperation *)operation
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"API call successful!"
message:@"Album updated successfully"
delegate:self
cancelButtonTitle:@"Ok"
otherButtonTitles:nil,
nil];
[alert show];
[alert release];
}
Get links to files and folders
Your apps can get the following types of links to OneDrive folders and files.
An embedded link, which is an HTML code snippet that you can insert into a webpage to provide an interactive view of the corresponding file.
Note
Currently, embedded links are supported only for files owned by the user. To determine whether a given file can be embedded, check to see if the corresponding File object's is_embeddable structure is set to true. Also, some embedded links provide a rich embedding experience, while others provide only an icon and a file name. Rich embedding is supported for the following types of Microsoft PowerPoint, Microsoft Excel, and Microsoft Word files: .doc, .docx, .pdf, .odt, .pot, .potm, .potx, .pps, .ppsm, .ppsx, .ppt, .pptm, .pptx, .xlsb, .xlsm, and .xlsx.
A read-only link, which is a link to a read-only version of the folder or file.
A read-write link, which is a link to a read-write version of the folder or file.
Note
When your apps request a read-only or read-write link to a file, the URL to that file is the same for either request. When your apps request a read-only link for a file and then later request a read-write link for that same file, read-only permission for that link is revoked. Similarly, when your apps request a read-write link for a file and then later request a read-only link for that same file, read-write permission for that link is revoked.
To get a link to a folder or file, use code like this.
-(void) getLinkToFile
{
[self.liveClient getWithPath:@"file.95d463b9efcca0e1.95D463B9EFCCA0E1!113/shared_read_link"
delegate:self
userState:@"onClickGetLinkToFileButton"];
}
- (void) liveOperationSucceeded:(LiveOperation *)operation
{
self.infoLabel.text = [NSString stringWithFormat: @"Shared read link = %@", [operation.result objectForKey:@"link"]];
}
In the preceding code, change the file ID to the folder ID or file ID that you want a link to. Also, change shared_read_link to one of these.
To embed for an embedded link. You get the embed HTML code snippet from the returned JavaScript Object Notation (JSON)-formatted object's embed_html structure value.
To shared_read_link for a read-only link. You get the read-only link from the returned JSON-formatted object's link structure value.
To shared_edit_link for a read-write link. You get the read-write link from the returned JSON-formatted object's link structure value.
When your app requests a file, photo, video, or audio, the returned object includes a preauthenticated URL as the value of the source attribute. Preauthenticated URLs allow access to a file without the need for additional permissions. Preauthenticated URLs expire and should not be stored or reused later. The following example shows what a preauthenticated URL looks like.
"source": "http://storage.live.com/s1pk0Hsk4twGIbzleNLcqhVhp5yvj9aPLPMHq2GDTRFBeuicbS27Fx5xBnmyy5J4lDSEtdvYm2FxOB0-s8Heb1l6hq3A1E2bJ9qWQ3GdIUTnnG6CHLTBLdCHRPUNAunekX1dnLS49vG-fUV7O8ZvrkiLTubXXFqrT1roxZ1oJjELE/Test.txt:Binary"
Summary
You have just learned how to add the ability read folder and file properties on OneDrive from your iOS apps. Next, you might want to allow users to upload or download files on OneDrive. To learn how to upload/download files on OneDrive, see Downloading and uploading files (iOS).