Home
%3CLINGO-SUB%20id%3D%22lingo-sub-287818%22%20slang%3D%22en-US%22%3EUpload%20and%20Download%20files%20from%20OneDrive%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-287818%22%20slang%3D%22en-US%22%3E%0A%20%26lt%3Bmeta%20http-equiv%3D%22Content-Type%22%20content%3D%22text%2Fhtml%3B%20charset%3DUTF-8%22%20%2F%26gt%3B%3CSTRONG%3E%20First%20published%20on%20MSDN%20on%20Sep%2024%2C%202014%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%3CP%3E%3C%2FP%3E%0A%20%20%3CP%3EHere%20is%20sample%20code%20to%20download%20and%20upload%20files%20to%20OneDrive%26nbsp%3B%20from%20Universal%20Windows%20Apps.%3C%2FP%3E%0A%20%20%3CP%3EComplete%20source%20code%20is%20available%20%3CA%20href%3D%22http%3A%2F%2Fcode.msdn.microsoft.com%2FUniversal-Windows-Apps-e750dff6%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3E%20here%3C%2FA%3E%3C%2FP%3E%0A%20%20%3CH2%20id%3D%22toc-hId-1766524464%22%20id%3D%22toc-hId-1793330687%22%3EIntroduction%20to%20Universal%20Windows%20Apps%3C%2FH2%3E%0A%20%20%3CP%3EVisual%20Studio%20template%20for%20Universal%20Windows%20Apps%20allows%20developers%20to%20build%20Windows%20Store%20App%20and%20Phone%20App%20in%20a%20single%20Visual%20Studio%20solution.%20This%20Visual%20Studio%20solution%20includes%20three%20projects%2C%20project%20for%20design%20and%20feature%20experiences%20unique%20to%20Store%20App%2C%20second%20project%20for%20Phone%20App%20and%20third%20project%20for%20shared%20code.%26nbsp%3B%20For%20more%20details%20on%20Universal%20Apps%20template%20in%20Visual%20Studio%20please%20click%20%3CA%20href%3D%22http%3A%2F%2Fblogs.msdn.com%2Fb%2Fvisualstudio%2Farchive%2F2014%2F04%2F14%2Fusing-visual-studio-to-build-universal-xaml-apps.aspx%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3E%20here%3C%2FA%3E%3C%2FP%3E%0A%20%20%3CP%3EHere%20is%20the%20Visual%20Studio%20projects%20for%20Universal%20Windows%20Apps.%3C%2FP%3E%0A%20%20%3CP%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F60039i462B792F074B4C22%22%20%2F%3E%3C%2FP%3E%0A%20%20%3CP%3EThe%20project%20ending%20with%20.Windows%20is%20for%20Store%20App%2C%20project%20ending%20with%20.WindowsPhone%20is%20for%20Phone%20App%20and%20.Shared%20is%20for%20shared%20code.%3C%2FP%3E%0A%20%20%3CP%3EIn%20the%20Shared%20Project%2C%20use%20following%20%23defines%20to%20write%20specific%20code%20for%20Phone%20App%20or%20Store%20App%3C%2FP%3E%0A%20%20%3CDIV%20id%3D%22scid%3A9D7513F9-C04C-4721-824A-2B34F0212519%3A527ae271-82e7-4f24-ad29-15b9a2110679%22%3E%0A%20%20%20%3CDIV%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%23if%20WINDOWS_PHONE_APP%20%2F%2F%20For%20Windows%20Phone%20App%20%23elif%20WINDOWS_APP%20%2F%2F%20For%20Windows%20Store%20App%20%23else%20%2F%2F%20Error%20%23endif%20%3CBR%20%2F%3E%3C%2FDIV%3E%3CBR%20%2F%3E%3C%2FDIV%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CH2%20id%3D%22toc-hId--785632497%22%20id%3D%22toc-hId--758826274%22%3EIntroduction%20to%20OneDrive%3C%2FH2%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CP%3EMicrosoft%20OneDrive%20is%20cloud-based%20storage%20solution%20for%20all%20your%20applications%E2%80%99%20end-users'%20information%2C%20files%2C%20and%20folders.%20For%20more%20details%20please%20click%20%3CA%20href%3D%22http%3A%2F%2Fmsdn.microsoft.com%2Fonedrive%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3E%20here%20%3C%2FA%3E%20.%3C%2FP%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CP%3EUse%20Azure%20Storage%20as%20cloud-based%20storage%20solution%20for%20all%20your%20applications%E2%80%99%20files%20that%20are%20common%20to%20all%20end-users.%20For%20more%20details%20please%20click%20%3CA%20href%3D%22http%3A%2F%2Fblogs.msdn.com%2Fb%2Fwebapps%2Farchive%2F2014%2F09%2F24%2Fupload-and-download-files-from-azure-storage.aspx%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3E%20here%20%3C%2FA%3E%20.%3C%2FP%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CH2%20id%3D%22toc-hId-957177838%22%20id%3D%22toc-hId-983984061%22%3ESteps%20to%20create%20Universal%20Windows%20App%3C%2FH2%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3COL%3E%3CBR%20%2F%3E%3CLI%3EOpen%20Visual%20Studio%3C%2FLI%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CLI%3EClick%20on%20File%20%7C%20New%3C%2FLI%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CLI%3ESelect%20Templates%20%7C%20Visual%20C%23%20%7C%20Store%20Apps%20%7C%20Universal%20Apps%20%7C%20Hub%20App%20as%20shown%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F60040i018AF9DE64642906%22%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3C%2FLI%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CLI%3ENow%20install%20NuGet%20Package%20for%20OneDrive%3C%2FLI%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CLI%3ERight%20click%20on%20the%20.Windows%20project%20and%20select%20Manage%20NuGet%20Packages%3C%2FLI%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CLI%3EIn%20the%20search%20textbox%20type%20Live%20SDK%20and%20select%20package%20created%20by%20Microsoft%20as%20shown%20below%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F60041i681DD3117B822413%22%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3C%2FLI%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CLI%3EClick%20on%20Install%20button%20for%20Live%20SDK%3C%2FLI%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CLI%3ENow%20install%20the%20OneDrive%20(Live%20SDK)%20NuGet%20Package%20on%20.WindowsPhone%20project%3C%2FLI%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CLI%3ERight%20click%20on%20the%20.WindowsPhone%20project%20and%20select%20Manage%20NuGet%20Packages%3C%2FLI%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CLI%3EIn%20the%20search%20textbox%20type%20Live%20SDK%20and%20click%20on%20the%20install%20button%20on%20package%20created%20by%20Microsoft%3C%2FLI%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CLI%3ENow%20lets%20right%20code%20for%20uploading%20file%20to%20OneDrive%3C%2FLI%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CLI%3EOpen%20App.xaml.cs%20file%20in%20the%20.Shared%20Project%3C%2FLI%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CLI%3ECopy%20this%20below%20upload%20function%20%3CBR%20%2F%3E%3CDIV%20id%3D%22scid%3A9D7513F9-C04C-4721-824A-2B34F0212519%3Ade170749-2a5b-4be9-bb7f-d7c1ed8a3699%22%3E%0A%20%20%20%20%20%3CDIV%3E%3CBR%20%2F%3E%20LiveConnectClient%20liveClient%3B%20%3CBR%20%2F%3Eprivate%20async%20Task%3CINT%3E%20UploadFileToOneDrive()%20%3CBR%20%2F%3E%7B%20%3CBR%20%2F%3Etry%20%3CBR%20%2F%3E%7B%20%3CBR%20%2F%3E%2F%2F%20create%20OneDrive%20auth%20client%20%3CBR%20%2F%3Evar%20authClient%20%3D%20new%20LiveAuthClient()%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%2F%2F%20ask%20for%20both%20read%20and%20write%20access%20to%20the%20OneDrive%20%3CBR%20%2F%3ELiveLoginResult%20result%20%3D%20await%20authClient.LoginAsync(new%20string%5B%5D%20%7B%20%22wl.skydrive%22%2C%20%22wl.skydrive_update%22%20%7D)%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%2F%2F%20if%20login%20successful%20%3CBR%20%2F%3Eif%20(result.Status%20%3D%3D%20LiveConnectSessionStatus.Connected)%20%3CBR%20%2F%3E%7B%20%3CBR%20%2F%3E%2F%2F%20create%20a%20OneDrive%20client%20%3CBR%20%2F%3EliveClient%20%3D%20new%20LiveConnectClient(result.Session)%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%2F%2F%20create%20a%20local%20file%20%3CBR%20%2F%3EStorageFile%20file%20%3D%20await%20ApplicationData.Current.LocalFolder.CreateFileAsync(%22filename%22%2C%20CreationCollisionOption.ReplaceExisting)%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%2F%2F%20copy%20some%20txt%20to%20local%20file%20%3CBR%20%2F%3EMemoryStream%20ms%20%3D%20new%20MemoryStream()%3B%20%3CBR%20%2F%3EDataContractSerializer%20serializer%20%3D%20new%20DataContractSerializer(typeof(string))%3B%20%3CBR%20%2F%3Eserializer.WriteObject(ms%2C%20%22Hello%20OneDrive%20World!!%22)%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3Eusing%20(Stream%20fileStream%20%3D%20await%20file.OpenStreamForWriteAsync())%20%3CBR%20%2F%3E%7B%20%3CBR%20%2F%3Ems.Seek(0%2C%20SeekOrigin.Begin)%3B%20%3CBR%20%2F%3Eawait%20ms.CopyToAsync(fileStream)%3B%20%3CBR%20%2F%3Eawait%20fileStream.FlushAsync()%3B%20%3CBR%20%2F%3E%7D%20%3CBR%20%2F%3E%3CBR%20%2F%3E%2F%2F%20create%20a%20folder%20%3CBR%20%2F%3Estring%20folderID%20%3D%20await%20GetFolderID(%22folderone%22)%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3Eif%20(string.IsNullOrEmpty(folderID))%20%3CBR%20%2F%3E%7B%20%3CBR%20%2F%3E%2F%2F%20return%20error%20%3CBR%20%2F%3Ereturn%200%3B%20%3CBR%20%2F%3E%7D%20%3CBR%20%2F%3E%3CBR%20%2F%3E%2F%2F%20upload%20local%20file%20to%20OneDrive%20%3CBR%20%2F%3Eawait%20liveClient.BackgroundUploadAsync(folderID%2C%20file.Name%2C%20file%2C%20OverwriteOption.Overwrite)%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3Ereturn%201%3B%20%3CBR%20%2F%3E%7D%20%3CBR%20%2F%3E%7D%20%3CBR%20%2F%3Ecatch%20%3CBR%20%2F%3E%7B%20%3CBR%20%2F%3E%7D%20%3CBR%20%2F%3E%2F%2F%20return%20error%20%3CBR%20%2F%3Ereturn%200%3B%20%3CBR%20%2F%3E%7D%3C%2FINT%3E%3C%2FDIV%3E%0A%20%20%20%20%3C%2FDIV%3E%3CBR%20%2F%3E%3C%2FLI%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CLI%3ECopy%20this%20below%20code%20to%20create%20a%20folder%20on%20OneDrive%20%3CBR%20%2F%3E%3CDIV%20id%3D%22scid%3A9D7513F9-C04C-4721-824A-2B34F0212519%3A27a276a1-57eb-4108-993b-9016844ffcbe%22%3E%0A%20%20%20%20%20%3CDIV%3E%3CBR%20%2F%3E%20public%20async%20Task%3CSTRING%3E%20GetFolderID(string%20folderName)%20%3CBR%20%2F%3E%7B%20%3CBR%20%2F%3Etry%20%3CBR%20%2F%3E%7B%20%3CBR%20%2F%3Estring%20queryString%20%3D%20%22me%2Fskydrive%2Ffiles%3Ffilter%3Dfolders%22%3B%20%3CBR%20%2F%3E%2F%2F%20get%20all%20folders%20%3CBR%20%2F%3ELiveOperationResult%20loResults%20%3D%20await%20liveClient.GetAsync(queryString)%3B%20%3CBR%20%2F%3Edynamic%20folders%20%3D%20loResults.Result%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3Eforeach%20(dynamic%20folder%20in%20folders.data)%20%3CBR%20%2F%3E%7B%20%3CBR%20%2F%3Eif%20(string.Compare(folder.name%2C%20folderName%2C%20StringComparison.OrdinalIgnoreCase)%20%3D%3D%200)%20%3CBR%20%2F%3E%7B%20%3CBR%20%2F%3E%2F%2F%20found%20our%20folder%20%3CBR%20%2F%3Ereturn%20folder.id%3B%20%3CBR%20%2F%3E%7D%20%3CBR%20%2F%3E%7D%20%3CBR%20%2F%3E%3CBR%20%2F%3E%2F%2F%20folder%20not%20found%20%3CBR%20%2F%3E%3CBR%20%2F%3E%2F%2F%20create%20folder%20%3CBR%20%2F%3EDictionary%3CSTRING%3E%20folderDetails%20%3D%20new%20Dictionary%3CSTRING%3E()%3B%20%3CBR%20%2F%3EfolderDetails.Add(%22name%22%2C%20folderName)%3B%20%3CBR%20%2F%3EloResults%20%3D%20await%20liveClient.PostAsync(%22me%2Fskydrive%22%2C%20folderDetails)%3B%20%3CBR%20%2F%3Efolders%20%3D%20loResults.Result%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%2F%2F%20return%20folder%20id%20%3CBR%20%2F%3Ereturn%20folders.id%3B%20%3CBR%20%2F%3E%7D%20%3CBR%20%2F%3Ecatch%20%3CBR%20%2F%3E%7B%20%3CBR%20%2F%3Ereturn%20string.Empty%3B%20%3CBR%20%2F%3E%7D%20%3CBR%20%2F%3E%7D%3C%2FSTRING%3E%3C%2FSTRING%3E%3C%2FSTRING%3E%3C%2FDIV%3E%0A%20%20%20%20%3C%2FDIV%3E%3CBR%20%2F%3E%3C%2FLI%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CLI%3ENow%20copy%20this%20below%20download%20function%20%3CBR%20%2F%3E%3CDIV%20id%3D%22scid%3A9D7513F9-C04C-4721-824A-2B34F0212519%3A6dec7aaf-f5e5-4849-80b8-e275fb67d10d%22%3E%0A%20%20%20%20%20%3CDIV%3E%3CBR%20%2F%3E%20public%20async%20Task%3CINT%3E%20DownloadFileFromOneDrive()%20%3CBR%20%2F%3E%7B%20%3CBR%20%2F%3Etry%20%3CBR%20%2F%3E%7B%20%3CBR%20%2F%3Estring%20fileID%20%3D%20string.Empty%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%2F%2F%20get%20folder%20ID%20%3CBR%20%2F%3Estring%20folderID%20%3D%20await%20GetFolderID(%22folderone%22)%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3Eif%20(string.IsNullOrEmpty(folderID))%20%3CBR%20%2F%3E%7B%20%3CBR%20%2F%3Ereturn%200%3B%20%2F%2F%20doesnt%20exists%20%3CBR%20%2F%3E%7D%20%3CBR%20%2F%3E%3CBR%20%2F%3E%2F%2F%20get%20list%20of%20files%20in%20this%20folder%20%3CBR%20%2F%3ELiveOperationResult%20loResults%20%3D%20await%20liveClient.GetAsync(folderID%20%2B%20%22%2Ffiles%22)%3B%20%3CBR%20%2F%3EList%3COBJECT%3E%20folder%20%3D%20loResults.Result%5B%22data%22%5D%20as%20List%3COBJECT%3E%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%2F%2F%20search%20for%20our%20file%20%3CBR%20%2F%3Eforeach%20(object%20fileDetails%20in%20folder)%20%3CBR%20%2F%3E%7B%20%3CBR%20%2F%3EIDictionary%3CSTRING%3E%20file%20%3D%20fileDetails%20as%20IDictionary%3CSTRING%3E%3B%20%3CBR%20%2F%3Eif%20(string.Compare(file%5B%22name%22%5D.ToString()%2C%20%22filename%22%2C%20StringComparison.OrdinalIgnoreCase)%20%3D%3D%200)%20%3CBR%20%2F%3E%7B%20%3CBR%20%2F%3E%2F%2F%20found%20our%20file%20%3CBR%20%2F%3EfileID%20%3D%20file%5B%22id%22%5D.ToString()%3B%20%3CBR%20%2F%3Ebreak%3B%20%3CBR%20%2F%3E%7D%20%3CBR%20%2F%3E%7D%20%3CBR%20%2F%3E%3CBR%20%2F%3Eif%20(string.IsNullOrEmpty(fileID))%20%3CBR%20%2F%3E%7B%20%3CBR%20%2F%3E%2F%2F%20file%20doesnt%20exists%20%3CBR%20%2F%3Ereturn%200%3B%20%3CBR%20%2F%3E%7D%20%3CBR%20%2F%3E%3CBR%20%2F%3E%2F%2F%20create%20local%20file%20%3CBR%20%2F%3EStorageFile%20localFile%20%3D%20await%20ApplicationData.Current.LocalFolder.CreateFileAsync(%22filename_from_onedrive%22%2C%20CreationCollisionOption.ReplaceExisting)%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%2F%2F%20download%20file%20from%20OneDrive%20%3CBR%20%2F%3Eawait%20liveClient.BackgroundDownloadAsync(fileID%20%2B%20%22%2Fcontent%22%2C%20localFile)%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3Ereturn%201%3B%20%3CBR%20%2F%3E%7D%20%3CBR%20%2F%3Ecatch%20%3CBR%20%2F%3E%7B%20%3CBR%20%2F%3E%7D%20%3CBR%20%2F%3Ereturn%200%3B%20%3CBR%20%2F%3E%7D%3C%2FSTRING%3E%3C%2FSTRING%3E%3C%2FOBJECT%3E%3C%2FOBJECT%3E%3C%2FINT%3E%3C%2FDIV%3E%0A%20%20%20%20%3C%2FDIV%3E%3CBR%20%2F%3E%3C%2FLI%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CLI%3ENow%20call%20these%20two%20functions%20%3CBR%20%2F%3E%3CDIV%20id%3D%22scid%3A9D7513F9-C04C-4721-824A-2B34F0212519%3A9ecfe426-a80e-476f-b582-76823f422419%22%3E%0A%20%20%20%20%20%3CDIV%3E%3CBR%20%2F%3E%20protected%20async%20override%20void%20OnLaunched(LaunchActivatedEventArgs%20e)%20%3CBR%20%2F%3E%7B%20%3CBR%20%2F%3E%23if%20DEBUG%20%3CBR%20%2F%3Eif%20(System.Diagnostics.Debugger.IsAttached)%20%3CBR%20%2F%3E%7B%20%3CBR%20%2F%3Ethis.DebugSettings.EnableFrameRateCounter%20%3D%20true%3B%20%3CBR%20%2F%3E%7D%20%3CBR%20%2F%3E%23endif%20%3CBR%20%2F%3Eint%20r%20%3D%20await%20UploadFileToOneDrive()%3B%20%3CBR%20%2F%3Eif(%20r%3D%3D%201)%20%3CBR%20%2F%3E%7B%20%3CBR%20%2F%3Eawait%20DownloadFileFromOneDrive()%3B%20%3CBR%20%2F%3E%7D%20%3CBR%20%2F%3E%3CBR%20%2F%3EFrame%20rootFrame%20%3D%20Window.Current.Content%20as%20Frame%3B%3C%2FDIV%3E%0A%20%20%20%20%3C%2FDIV%3E%3CBR%20%2F%3E%3C%2FLI%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CLI%3ERun%20the%20Windows%20Store%20App%3C%2FLI%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CLI%3EDid%20you%20get%20any%20errors%20%3F%20%3CBR%20%2F%3E%3CDIV%20id%3D%22scid%3A9D7513F9-C04C-4721-824A-2B34F0212519%3Abd06067c-b6a0-4582-9493-4448f039c1d1%22%3E%0A%20%20%20%20%20%3CDIV%3E%3CBR%20%2F%3E%20%7B%22Object%20reference%20not%20set%20to%20an%20instance%20of%20an%20object.%22%7D%20%3CBR%20%2F%3E%3CBR%20%2F%3Eat%20Microsoft.Live.ResourceHelper.GetString(String%20name)%20%3CBR%20%2F%3Eat%20Microsoft.Live.TailoredAuthClient.%3CAUTHENTICATEASYNC%3Ed__0.MoveNext()%20%3CBR%20%2F%3E---%20End%20of%20stack%20trace%20from%20previous%20location%20where%20exception%20was%20thrown%20---%20%3CBR%20%2F%3Eat%20System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task%20task)%20%3CBR%20%2F%3Eat%20System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task%20task)%20%3CBR%20%2F%3Eat%20System.Runtime.CompilerServices.TaskAwaiter%601.GetResult()%20%3CBR%20%2F%3Eat%20Microsoft.Live.LiveAuthClient.%3CEXECUTEAUTHTASKASYNC%3Ed__8.MoveNext()%20%3CBR%20%2F%3E---%20End%20of%20stack%20trace%20from%20previous%20location%20where%20exception%20was%20thrown%20---%20%3CBR%20%2F%3Eat%20System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task%20task)%20%3CBR%20%2F%3Eat%20System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task%20task)%20%3CBR%20%2F%3Eat%20System.Runtime.CompilerServices.TaskAwaiter%601.GetResult()%20%3CBR%20%2F%3Eat%20UniversalAppOneDrive.App.%3CUPLOADFILETOONEDRIVE%3Ed__a.MoveNext()%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3C%2FUPLOADFILETOONEDRIVE%3E%3C%2FEXECUTEAUTHTASKASYNC%3E%3C%2FAUTHENTICATEASYNC%3E%3C%2FDIV%3E%0A%20%20%20%20%3C%2FDIV%3E%3CBR%20%2F%3E%3C%2FLI%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CLI%3EThis%20error%20usually%20indicates%20our%20current%20Windows%20Store%20App%20is%20not%20associated%20to%20an%20App%20Name%20in%20the%20store%3C%2FLI%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CLI%3ETo%20associate%20our%20current%20app%20with%20the%20Store%2C%20right%20click%20on%20.Windows%20project%2C%20select%20Store%20%7C%20Associate%20App%20with%20the%20Store%E2%80%A6%3C%2FLI%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CLI%3ESign%20in%20with%20your%20developer%20account%20and%20register%20the%20app%20name%3C%2FLI%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CLI%3ENow%20run%20the%20Windows%20Store%20App%20again%3C%2FLI%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CLI%3EThis%20time%20Microsoft%20Account%20UI%20will%20ask%20end%20user%20permission%20to%20allow%20your%20app%20to%20access%20their%20OneDrive%20as%20shown%20below%3C%2FLI%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CLI%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F60042iAEBB0C058C7D1752%22%20%2F%3E%3C%2FLI%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CLI%3E%3CA%20href%3D%22http%3A%2F%2Fcode.msdn.microsoft.com%2FUniversal-Windows-Apps-e750dff6%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3Ehere%20%3C%2FA%3E%20is%20the%20complete%20source%20code%3C%2FLI%3E%3CBR%20%2F%3E%3C%2FOL%3E%0A%20%0A%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-287818%22%20slang%3D%22en-US%22%3EFirst%20published%20on%20MSDN%20on%20Sep%2024%2C%202014%20%26nbsp%3B%20Here%20is%20sample%20code%20to%20download%20and%20upload%20files%20to%20OneDrive%26nbsp%3B%20from%20Universal%20Windows%20Apps.%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-287818%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3Euniversal%20windows%20apps%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Microsoft
First published on MSDN on Sep 24, 2014

Here is sample code to download and upload files to OneDrive  from Universal Windows Apps.

Complete source code is available here

Introduction to Universal Windows Apps

Visual Studio template for Universal Windows Apps allows developers to build Windows Store App and Phone App in a single Visual Studio solution. This Visual Studio solution includes three projects, project for design and feature experiences unique to Store App, second project for Phone App and third project for shared code.  For more details on Universal Apps template in Visual Studio please click here

Here is the Visual Studio projects for Universal Windows Apps.

The project ending with .Windows is for Store App, project ending with .WindowsPhone is for Phone App and .Shared is for shared code.

In the Shared Project, use following #defines to write specific code for Phone App or Store App



#if WINDOWS_PHONE_APP
// For Windows Phone App
#elif WINDOWS_APP
// For Windows Store App
#else
// Error
#endif



Introduction to OneDrive



Microsoft OneDrive is cloud-based storage solution for all your applications’ end-users' information, files, and folders. For more details please click here .



Use Azure Storage as cloud-based storage solution for all your applications’ files that are common to all end-users. For more details please click here .



Steps to create Universal Windows App







  1. Open Visual Studio


  2. Click on File | New


  3. Select Templates | Visual C# | Store Apps | Universal Apps | Hub App as shown











  4. Now install NuGet Package for OneDrive


  5. Right click on the .Windows project and select Manage NuGet Packages


  6. In the search textbox type Live SDK and select package created by Microsoft as shown below











  7. Click on Install button for Live SDK


  8. Now install the OneDrive (Live SDK) NuGet Package on .WindowsPhone project


  9. Right click on the .WindowsPhone project and select Manage NuGet Packages


  10. In the search textbox type Live SDK and click on the install button on package created by Microsoft


  11. Now lets right code for uploading file to OneDrive


  12. Open App.xaml.cs file in the .Shared Project


  13. Copy this below upload function

    LiveConnectClient liveClient;
    private async Task<int> UploadFileToOneDrive()
    {
    try
    {
    // create OneDrive auth client
    var authClient = new LiveAuthClient();

    // ask for both read and write access to the OneDrive
    LiveLoginResult result = await authClient.LoginAsync(new string[] { "wl.skydrive", "wl.skydrive_update" });

    // if login successful
    if (result.Status == LiveConnectSessionStatus.Connected)
    {
    // create a OneDrive client
    liveClient = new LiveConnectClient(result.Session);

    // create a local file
    StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync("filename", CreationCollisionOption.ReplaceExisting);

    // copy some txt to local file
    MemoryStream ms = new MemoryStream();
    DataContractSerializer serializer = new DataContractSerializer(typeof(string));
    serializer.WriteObject(ms, "Hello OneDrive World!!");

    using (Stream fileStream = await file.OpenStreamForWriteAsync())
    {
    ms.Seek(0, SeekOrigin.Begin);
    await ms.CopyToAsync(fileStream);
    await fileStream.FlushAsync();
    }

    // create a folder
    string folderID = await GetFolderID("folderone");

    if (string.IsNullOrEmpty(folderID))
    {
    // return error
    return 0;
    }

    // upload local file to OneDrive
    await liveClient.BackgroundUploadAsync(folderID, file.Name, file, OverwriteOption.Overwrite);

    return 1;
    }
    }
    catch
    {
    }
    // return error
    return 0;
    }



  14. Copy this below code to create a folder on OneDrive

    public async Task<string> GetFolderID(string folderName)
    {
    try
    {
    string queryString = "me/skydrive/files?filter=folders";
    // get all folders
    LiveOperationResult loResults = await liveClient.GetAsync(queryString);
    dynamic folders = loResults.Result;

    foreach (dynamic folder in folders.data)
    {
    if (string.Compare(folder.name, folderName, StringComparison.OrdinalIgnoreCase) == 0)
    {
    // found our folder
    return folder.id;
    }
    }

    // folder not found

    // create folder
    Dictionary<string, object> folderDetails = new Dictionary<string, object>();
    folderDetails.Add("name", folderName);
    loResults = await liveClient.PostAsync("me/skydrive", folderDetails);
    folders = loResults.Result;

    // return folder id
    return folders.id;
    }
    catch
    {
    return string.Empty;
    }
    }



  15. Now copy this below download function

    public async Task<int> DownloadFileFromOneDrive()
    {
    try
    {
    string fileID = string.Empty;

    // get folder ID
    string folderID = await GetFolderID("folderone");

    if (string.IsNullOrEmpty(folderID))
    {
    return 0; // doesnt exists
    }

    // get list of files in this folder
    LiveOperationResult loResults = await liveClient.GetAsync(folderID + "/files");
    List<object> folder = loResults.Result["data"] as List<object>;

    // search for our file
    foreach (object fileDetails in folder)
    {
    IDictionary<string, object> file = fileDetails as IDictionary<string, object>;
    if (string.Compare(file["name"].ToString(), "filename", StringComparison.OrdinalIgnoreCase) == 0)
    {
    // found our file
    fileID = file["id"].ToString();
    break;
    }
    }

    if (string.IsNullOrEmpty(fileID))
    {
    // file doesnt exists
    return 0;
    }

    // create local file
    StorageFile localFile = await ApplicationData.Current.LocalFolder.CreateFileAsync("filename_from_onedrive", CreationCollisionOption.ReplaceExisting);

    // download file from OneDrive
    await liveClient.BackgroundDownloadAsync(fileID + "/content", localFile);

    return 1;
    }
    catch
    {
    }
    return 0;
    }



  16. Now call these two functions

    protected async override void OnLaunched(LaunchActivatedEventArgs e)
    {
    #if DEBUG
    if (System.Diagnostics.Debugger.IsAttached)
    {
    this.DebugSettings.EnableFrameRateCounter = true;
    }
    #endif
    int r = await UploadFileToOneDrive();
    if( r== 1)
    {
    await DownloadFileFromOneDrive();
    }

    Frame rootFrame = Window.Current.Content as Frame;



  17. Run the Windows Store App


  18. Did you get any errors ?

    {"Object reference not set to an instance of an object."}

    at Microsoft.Live.ResourceHelper.GetString(String name)
    at Microsoft.Live.TailoredAuthClient.<AuthenticateAsync>d__0.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
    at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
    at Microsoft.Live.LiveAuthClient.<ExecuteAuthTaskAsync>d__8.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
    at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
    at UniversalAppOneDrive.App.<UploadFileToOneDrive>d__a.MoveNext()




  19. This error usually indicates our current Windows Store App is not associated to an App Name in the store


  20. To associate our current app with the Store, right click on .Windows project, select Store | Associate App with the Store…


  21. Sign in with your developer account and register the app name


  22. Now run the Windows Store App again


  23. This time Microsoft Account UI will ask end user permission to allow your app to access their OneDrive as shown below




  24. here is the complete source code