2014年2月8日 星期六

[Windows Store] 在Windows Store APP中使用Skydrive API操作資料夾(CRUD)

上一篇介紹了Windows Store APP中使用Live SDK的登入,這篇就來看下Live SDK中關於Skydrive API的部分,以下就以操作Skydrive資料夾的新增、讀取、更新、刪除(CRUD)為範例:

 

介面部分很簡單就是放一個登入鈕跟Create、Get、Update、Delete(CRUD)四個按鈕,旁邊的Listbox則用來顯示讀取到的所有資料夾,而登入部分的程式碼可以參考上一篇文章,這裡就不再重複說明

image


<Page
x:Class="LiveSDKSample.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:LiveSDKSample"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:live="using:Microsoft.Live.Controls"
mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Button Content="Sign In" Height="151" Margin="307,107,0,0" VerticalAlignment="Top" Width="287" Click="LoginButton_Click"/>
<Button Name="CreateButton" Content="Create Folder" Height="77" Margin="706,66,0,0" VerticalAlignment="Top" Width="193" Click="CreateButton_Click"/>
<Button Name="GetButton" Content="Get Folders" Height="77" Margin="706,181,0,0" VerticalAlignment="Top" Width="193" Click="GetButton_Click"/>
<Button Name="DeleteButton" Content="Delete Folder" Height="77" Margin="706,384,0,0" VerticalAlignment="Top" Width="193" Click="DeleteButton_Click"/>
<Button x:Name="UpdateButton" Content="Update Folder" Height="77" Margin="706,287,0,0" VerticalAlignment="Top" Width="193" Click="UpdateButton_Click"/>
<ListBox Name="FolderBox" HorizontalAlignment="Left" Height="312" Margin="984,88,0,0" VerticalAlignment="Top" Width="298"/>
</Grid>
</Page>


新增資料夾

要成功新增資料夾,必須記得在登入時要跟使用者取得wl.skydrive_update的權限,程式碼如下:

private async void CreateButton_Click(object sender, RoutedEventArgs e)
{
try
{
Dictionary<string, object> newfolder = new Dictionary<string, object>();
newfolder.Add("name", "testfolder"); //testfolder是要建立的資料夾名稱
LiveOperationResult operationresult = await liveClient.PostAsync("me/skydrive", newfolder);
MessageDialog msg = new MessageDialog(string.Format("FolderName:{0}\nID:{1}",
operationresult.Result["name"],
operationresult.Result["id"]
));
await msg.ShowAsync();
}
catch (LiveConnectException ex)
{
MessageDialog msg = new MessageDialog(ex.Message);
msg.ShowAsync();
}
}


讀取資料夾

讀取資料夾記得要取得wl.skydrive權限,這邊範例是取得所有的資料夾資訊並顯示在Listbox中,而把ID值記在Tag屬性中則是讓選中的資料夾可以做update跟delete,如果只是要取得某個資料夾只要在GetAsync()方法中傳入資料夾ID即可

private async void GetButton_Click(object sender, RoutedEventArgs e)
{
try
{
LiveOperationResult operationResult = await liveClient.GetAsync("me/skydrive/files");
List<object> data = (List<object>)operationResult.Result["data"];
FolderBox.Items.Clear();
foreach (dynamic item in data)
{
//將資料夾的ID記在Tag中方便做update和delete
FolderBox.Items.Add(new ListBoxItem()
{
Content = item.name,
Tag = item.id
});
}
}
catch (LiveConnectException ex)
{
MessageDialog msg = new MessageDialog(ex.Message);
msg.ShowAsync();
}
}


在Listbox中顯示讀取的資料夾

image

 

更新資料夾

更新資料夾要wl.skydrive_update權限,此範例中會將Listbox中選中的資料夾更新名字,而需要傳入的ID就是剛剛記在Tag屬性中的值

private async void UpdateButton_Click(object sender, RoutedEventArgs e)
{
try
{
if (FolderBox.SelectedItem != null)
{
Dictionary<string, object> updatefolder = new Dictionary<string, object>();
updatefolder.Add("name", "updatefolder");
//PutAsync中傳入要更新的資料夾ID
LiveOperationResult operationresult = await liveClient.PutAsync((FolderBox.SelectedItem as ListBoxItem).Tag.ToString(), updatefolder);
MessageDialog msg = new MessageDialog("Folder updated");
await msg.ShowAsync();
}
}
catch (LiveConnectException ex)
{
MessageDialog msg = new MessageDialog(ex.Message);
msg.ShowAsync();
}
}


刪除資料夾

刪除資料夾要wl.skydrive_update權限,一樣也是刪除Listbox中選中的資料夾

private async void DeleteButton_Click(object sender, RoutedEventArgs e)
{
try
{
if (FolderBox.SelectedItem != null)
{
//DeleteAsync中傳入要刪除的資料夾ID
LiveOperationResult operationresult = await liveClient.DeleteAsync((FolderBox.SelectedItem as ListBoxItem).Tag.ToString());
MessageDialog msg = new MessageDialog("Folder deleted");
await msg.ShowAsync();
}
}
catch (LiveConnectException ex)
{
MessageDialog msg = new MessageDialog(ex.Message);
msg.ShowAsync();
}
}


完整的程式碼如下

using System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Microsoft.Live;
using Windows.UI.Popups;
using System.Collections.Generic;
namespace LiveSDKSample
{
public sealed partial class MainPage : Page
{
LiveConnectClient liveClient;
public MainPage()
{
this.InitializeComponent();
}
private async void LoginButton_Click(object sender, RoutedEventArgs e)
{
try
{
LiveAuthClient auth = new LiveAuthClient();
LiveLoginResult loginresult = await auth.LoginAsync(new string[]
{
"wl.signin",
"wl.basic",
"wl.skydrive",
"wl.skydrive_update"
});
if (loginresult.Status == LiveConnectSessionStatus.Connected)
{
liveClient = new LiveConnectClient(loginresult.Session);
LiveOperationResult operationresult = await liveClient.GetAsync("me");
MessageDialog msg = new MessageDialog(string.Format("Name:{0}",
operationresult.Result["name"]
));
await msg.ShowAsync();
}
}
catch (LiveAuthException ex)
{
MessageDialog msg = new MessageDialog(ex.Message);
msg.ShowAsync();
}
}
private async void CreateButton_Click(object sender, RoutedEventArgs e)
{
try
{
Dictionary<string, object> newfolder = new Dictionary<string, object>();
newfolder.Add("name", "testfolder"); //testfolder是要建立的資料夾名稱
LiveOperationResult operationresult = await liveClient.PostAsync("me/skydrive", newfolder);
MessageDialog msg = new MessageDialog(string.Format("FolderName:{0}\nID:{1}",
operationresult.Result["name"],
operationresult.Result["id"]
));
await msg.ShowAsync();
}
catch (LiveConnectException ex)
{
MessageDialog msg = new MessageDialog(ex.Message);
msg.ShowAsync();
}
}
private async void GetButton_Click(object sender, RoutedEventArgs e)
{
try
{
LiveOperationResult operationResult = await liveClient.GetAsync("me/skydrive/files");
List<object> data = (List<object>)operationResult.Result["data"];
FolderBox.Items.Clear();
foreach (dynamic item in data)
{
//將資料夾的ID記在Tag中方便做update和delete
FolderBox.Items.Add(new ListBoxItem()
{
Content = item.name,
Tag = item.id
});
}
}
catch (LiveConnectException ex)
{
MessageDialog msg = new MessageDialog(ex.Message);
msg.ShowAsync();
}
}
private async void DeleteButton_Click(object sender, RoutedEventArgs e)
{
try
{
if (FolderBox.SelectedItem != null)
{
//DeleteAsync中傳入要刪除的資料夾ID
LiveOperationResult operationresult = await liveClient.DeleteAsync((FolderBox.SelectedItem as ListBoxItem).Tag.ToString());
MessageDialog msg = new MessageDialog("Folder deleted");
await msg.ShowAsync();
}
}
catch (LiveConnectException ex)
{
MessageDialog msg = new MessageDialog(ex.Message);
msg.ShowAsync();
}
}
private async void UpdateButton_Click(object sender, RoutedEventArgs e)
{
try
{
if (FolderBox.SelectedItem != null)
{
Dictionary<string, object> updatefolder = new Dictionary<string, object>();
updatefolder.Add("name", "updatefolder");
//PutAsync中傳入要更新的資料夾ID
LiveOperationResult operationresult = await liveClient.PutAsync((FolderBox.SelectedItem as ListBoxItem).Tag.ToString(), updatefolder);
MessageDialog msg = new MessageDialog("Folder updated");
await msg.ShowAsync();
}
}
catch (LiveConnectException ex)
{
MessageDialog msg = new MessageDialog(ex.Message);
msg.ShowAsync();
}
}
}
}


Reference

SkyDrive API

使用 Microsoft SkyDrive 資料夾和檔案

0 意見:

張貼留言