Wp8.1: onedrive operation

Source: Internet
Author: User

Xiaomeng today shares with you some of the operations in the Development of onedrive in Windows Phone 8.1:

  • Onedrive login in Windows Phone 8.1
  • Upload and update onedrive files/photos in Windows Phone 8.1
  • Onedrive file/photo download in Windows Phone 8.1
  • Create, delete, move, and copy the onedrive folder in Windows Phone 8.1
  • Some key concepts of onedrive in Windows Phone 8.1

Finally, the source code is available.

Download live SDK for Windows Phone 8.1:

Go to the http://msdn.microsoft.com/onedrive/dn630256 page and select live SDK downloads for Windows, Windows Phone, And. Net to install. Reference the live SDK after installation. Don't worry. It's not over yet.

Associate an application with an App Store:

In the toolbar App Store, select to associate the app with the app store. Follow the prompts to complete the operation. Okay. Now let's get started.

Onedrive login in Windows Phone 8.1:

Now, let's start with the code.

Liveloginresult result; private async void loginbutton_click (Object sender, routedeventargs e) // log on to {try {msgtext. TEXT = "parent: logging on"; var authclient = new liveauthclient (); // initialization, used to obtain user data. Result = await authclient. loginasync (New String [] {"WL. signin "," WL. skyDrive "," WL. skydrive_update "," WL. photos "}); // log on. The parameter is scopes if (result. status = liveconnectsessionstatus. connected) {var connectclient = new liveconnectclient (result. session); var meresult = await connectclient. getasync ("me"); msgtext. TEXT = "Dear:" + meresult. result ["name"]. tostring () + "you have successfully logged on to onedrive! ";}} Catch (liveauthexception ex) {msgtext. Text = ex. Message;} catch (liveconnectexception ex) {msgtext. Text = ex. Message ;}}
Scopes:

Scopes is translated into a domain. Authorization allows an application to obtain user data. Domain is divided into core domain and extended domain: core domain includes WL. Basic: Get the basic information of the user, you can also get the user's contact. WL. offline_access: You can read and update user data at any time. WL. signin: Single Sign-On. That is, as long as the user has logged on, you only need to authorize the user and do not need to log on again. Windows Phone is recommended. Extended domain: There are many extended domains. Commonly Used: WL. Photos: authorize users to obtain users' images, videos, album sets, audios, etc. WL. SkyDrive: read user files stored on onedrive WL. skydrive_update: Upload, modify files stored on onedrive

JSON data format of basic user information:

{"ID": "8c8ce076ca27823f", "name": "Robert Tamburello", "first_name": "Robert to", "last_name": "Tamburello", "gender": NULL, "locale": "en_us"} is the meresult of the previous example. result.

In Windows Phone 8.1, onedrive uploads files:
Private async void uploadbutton_click (Object sender, routedeventargs e) // upload {try {msgtext. Text = ": Uploading"; if (result. session! = NULL) {var liveconnectclient = new liveconnectclient (result. session); // read the file storagefolder localfolder = applicationdata. current. localfolder; storagefile file = await localfolder. getfileasync (App. testfilename); liveuploadoperation uploadoperation = await liveconnectclient. createbackgrounduploadasync ("me/SkyDrive", file. name, file, overwriteoption. rename); liveoperationresult uploadresult = await uploadoperation. startasync () ;}} catch (liveauthexception ex) {msgtext. TEXT = ex. message;} catch (liveconnectexception ex) {msgtext. TEXT = ex. message ;}}
Download the onedrive file in Windows Phone 8.1:

We know that the file to be downloaded is test.txt. That is, the uploaded file.

Private async void downbutton_click_1 (Object sender, routedeventargs e) // download {try {msgtext. TEXT = ": Downloading"; string id = string. empty; If (result. session! = NULL) {var connectclient = new liveconnectclient (result. Session); liveoperationresult operationresult = await connectclient. getasync ("me/SkyDrive/search? Qinitest.txt "); List <Object> items = operationresult. result ["data"] As list <Object>; idictionary <string, Object> file = items. first () as idictionary <string, Object>; id = file ["ID"]. tostring (); livedownloadoperation operation = await connectclient. createbackgrounddownloadasync (string. format ("{0}/content", ID); var Results = await operation. startasync (); string strings = results. stream. tostring (); Stor Agefolder localfolder = applicationdata. current. localfolder; storagefile files = await localfolder. getfileasync (App. testfilename); await fileio. writetextasync (files, strings); msgtext. TEXT = "congratulations: You have successfully downloaded the file from onedrive! ";}} Catch (exception ex) {msgtext. Text = ex. Message ;}}
Description of request parameters:

Filter: Specify the project type (all (default type), photos, videos, audio, folders, or albums) to obtain only some types of projects. For example, to retrieve only photos, use folder_id/files? Filter = photos. Limit: specify the number of projects to be obtained to obtain a limited number of projects. For example, to obtain the first two projects, use folder_id/files? Limit = 2. Offset: Set the offset parameter to the index of the first item to be retrieved to specify the first item to be retrieved. For example, to obtain two projects starting from the third project, use folder_id/files? Limit = 2 & offset = 3. Q: get the file with the specified name. For example, in the above example: Me/SkyDrive/search? Q6. test.txt

In Windows Phone 8.1, onedrive uploads images:

Uploading images and files is the same method:

Private async void uploadpiactrue_click (Object sender, routedeventargs e) {storagefile file = await windows. applicationModel. package. current. installedlocation. getfileasync ("2.jpg"); try {msgtext. TEXT = "parent: Uploading"; if (result. session! = NULL) {var liveconnectclient = new liveconnectclient (result. session); liveuploadoperation uploadoperation = await liveconnectclient. createbackgrounduploadasync ("me/SkyDrive/camera_roll", file. name, file, overwriteoption. overwrite); liveoperationresult uploadresult = await uploadoperation. startasync (); If (uploadresult. result! = NULL) {msgtext. Text = "congratulations: You have uploaded onedrive! ";}} Catch (liveauthexception ex) {msgtext. Text = ex. Message;} catch (liveconnectexception ex) {msgtext. Text = ex. Message ;}}
Onedrive uses friendly names to access specific folders:

To access a specific onedrive folder, you can use a friendly name instead of a folder ID. You can use the following friendly names in the onedrive UI to access the corresponding folder: • user_id/SkyDrive/camera_roll indicates the onedrive local photos folder. • User_id/SkyDrive/my_documents indicates the "document" folder. • User_id/SkyDrive/my_photos indicates the "image" folder. • User_id/SkyDrive/public_documents indicates the "public" folder. In general, replace user_id with me (for logged-on users ). For the above example, upload the image to the local photo folder.

Download the image from onedrive in Windows Phone 8.1:
Private async void downpictrue_click (Object sender, routedeventargs e) {msgtext. TEXT = ": Downloading"; string idpictrue = string. empty; string namepictrue = string. empty; try {liveconnectclient liveclient = new liveconnectclient (result. session); liveoperationresult operationresult = await liveclient. getasync ("me/SkyDrive/camera_roll"); Dynamic Results = operationresult. result; string idfolder = results. ID; liveoperationresult pictrueresult = await liveclient. getasync (idfolder + "/photos"); Dynamic pictrues = pictrueresult. result; foreach (Dynamic pictrue in pictrues. data) {namepictrue = pictrue. name; idpictrue = pictrue. ID; storagefile pictruefile = await applicationdata. current. localfolder. createfileasync (namepictrue, creationcollisionoption. replaceexisting); await liveclient. backgrounddownloadas YNC (idpictrue + "/picture? Type = thumbnail ", pictruefile); var imgsource = new bitmapimage (); var stream = await pictruefile. openreadasync (); imgsource. setsource (Stream); imgresult. source = imgsource;} msgtext. TEXT = "congratulations: You have successfully downloaded the image from onedrive! ";} Catch (liveconnectexception exception) {msgtext. Text =" error getting album info: "+ exception. Message ;}
Onedrive project preview:

To preview the onedrive project, go to/SkyDrive/get_item_preview? Type = type & url = URL: Send a GET request. The optional type query string parameter is one of the following values: • Thumbnail (thumbnail) • Small (for maximum 100x100 pixel Preview) • album (maximum 200x200) • normal (max. 800x800)

Create a folder on onedrive in Windows Phone 8.1:
Private async void creatfodel_click (Object sender, routedeventargs e) {try {If (result. session! = NULL) {var folderdata = new dictionary <string, Object> (); folderdata. add ("name", "test"); liveconnectclient liveclient = new liveconnectclient (result. session); liveoperationresult operationresult = await liveclient. postasync ("me/SkyDrive", folderdata); msgtext. TEXT = string. join ("", "new folder", operationresult. result ["name"]. tostring (), "ID:", operationresult. result ["ID"]. tostring () ;}} catch (liveconnectexception exception) {msgtext. TEXT = "error creating Folder:" + exception. message ;}}
In Windows Phone 8.1, onedrive deletes a folder:
Private async void deletefodel_click (Object sender, routedeventargs e) {string id = string. empty; liveconnectclient liveclient = new liveconnectclient (result. session); liveoperationresult operationresult = await liveclient. getasync ("me/SkyDrive/search? Q = test "); List <Object> items = operationresult. result ["data"] As list <Object>; idictionary <string, Object> file = items. first () as idictionary <string, Object>; id = file ["ID"]. tostring (); liveoperationresult operationresult1 = await liveclient. deleteasync (ID); msgtext. TEXT = "folder deleted ";}
In Windows Phone 8.1, move onedrive and copy the folder:
LiveOperationResult operationResult =await moveFileClient.MoveAsync("file.9d24497f7fef8f33.9D24497F7FEF8F33!748","folder.9d24497f7fef8f33.9D24497F7FEF8F33!265");

The first parameter is the ID of the file to be moved, and the second parameter is the ID of the file to be moved. Replace moveasync with copeasync, and change the two parameters to the ID of the file or folder you want to copy. Liveconnectclient. getasync must pay attention to two structures in the returned JavaScript Object Notation (JSON) Format: Id structure, indicating the folder ID of the top-level folder. If you send a request to me/SkyDrive/files, the returned JSON object only contains information about all subfolders, album sets, and files in the top-level folder of the user. To obtain the list of most commonly used onedrive documents, use WL. skyDrive scope sends GET requests to/user_id/SkyDrive/recent_docs

Get your total onedrive storage quota and remaining storage quota

To about the available and unused buckets in onedrive, call folder_id/files by get to/ME/SkyDrive/quota or/user_id/SkyDrive/quota, folder_id indicates a valid folder ID. You can obtain the list of all projects in the folder and set the project sorting conditions by specifying the following conditions using the sort_by parameter in the code above: updated, name, size, or default. For example, to sort projects by name, use folder_id/files? Sort_by = Name. You can use the sort_order parameter in the above Code to specify the following order to set the sorting order of projects: ascending or descending. For example, to sort projects in descending order, use folder_id/files? Sort_order = descending.

Onedrive album in Windows Phone 8.1:

• Me/albums to  about all the album sets of logged-on users. •/ME/SkyDrive/shared/albums to  about all shared album sets of logged-on users. • User_id/albums can be used to  about all the albums corresponding to valid user_id. • Use album_id/photos or album_id/videos to obtain a list of all photos or videos in the album. Then, your code can be called to obtain the corresponding photo or video information using a specific photo ID or video ID. • Use album_id/files to obtain a list of all photos, videos, audios, subalbum sets, and subfolders in the album. Then, your code can make another call, this call uses a specific photo ID, video ID, album ID, audio ID, or Folder Id to  about a photo, video, album, audio, or folder. • Use/picture with the optional type parameter in the above Code in combination with one of the following values to obtain pictures or videos: small and normal (default value when the type parameter is not specified), album, thumbnail, or full. For example, to obtain a thumbnail of a photo, use photo_id/picture? Type = thumbnail. • Obtain the video content by using video_id/video.

In Windows Phone 8.1, onedrive traverses files:

Traverse all the files in onedrive and obtain the specified files.

await connectClient.GetAsync(“me/skydrive/files”);List<object> items = operationResult.Result["data"] as List<object>;foreach (object item in items)     {IDictionary<string, object> file = item as IDictionary<string, object>;if (file["name"].ToString() == “WorkBook.xml”){                id = file["id"].ToString();                  }}
Source code for onedrive in Windows Phone 8.1:

Http://www.bcmeng.com/bbs/forum.php? MoD = viewthread & tid = 188 & extra = Page % 3d1Hope you can use the website advertisement to support Xiaomeng. Thank you!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.