Skip to content
This repository was archived by the owner on Nov 16, 2023. It is now read-only.

Commit 16a0d84

Browse files
committed
Added snippet for large file upload. Don't merge this into this sample master until microsoftgraph/msgraph-sdk-dotnet#76 is merged into master of the client library, and after the Nuget 1.2.1 package has been uploaded, https://www.nuget.org/packages/Microsoft.Graph
1 parent 2813883 commit 16a0d84

9 files changed

+108
-42
lines changed

Microsoft-Graph-Snippets-SDK/App.xaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ See LICENSE in the project root for license information. -->
1111
<Application.Resources>
1212

1313
<!-- Add your client id here. -->
14-
<x:String x:Key="ida:ClientID"></x:String>
14+
<x:String x:Key="ida:ClientID">347afbd6-9fb3-4762-94c6-86a445b0a40d</x:String>
1515
<!-- Add your return URL here. -->
16-
<x:String x:Key="ida:ReturnUrl"></x:String>
16+
<x:String x:Key="ida:ReturnUrl">urn:ietf:wg:oauth:2.0:oob</x:String>
1717

1818
</Application.Resources>
1919

Microsoft-Graph-Snippets-SDK/Assets/Resources.resw

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,9 @@
261261
<data name="UpdateXLRange" xml:space="preserve">
262262
<value>Update Range</value>
263263
</data>
264+
<data name="UploadLargeFile" xml:space="preserve">
265+
<value>Upload Large File</value>
266+
</data>
264267
<data name="UploadXLFile" xml:space="preserve">
265268
<value>Upload File</value>
266269
</data>
Binary file not shown.

Microsoft-Graph-Snippets-SDK/MainPage.xaml.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ private void CreateStoryList()
6767
var usersGroupName = ResourceLoader.GetForCurrentView().GetString("UsersGroup");
6868
var groupsGroupName = ResourceLoader.GetForCurrentView().GetString("GroupsGroup");
6969

70+
StoryCollection.Add(new StoryDefinition() { GroupName = usersGroupName, Title = ResourceLoader.GetForCurrentView().GetString("UploadLargeFile"), ScopeGroup = ScopeGroupAll, RunStoryAsync = UserStories.TryUploadLargeFileAsync });
7071
StoryCollection.Add(new StoryDefinition() { GroupName = usersGroupName, Title = ResourceLoader.GetForCurrentView().GetString("GetMe"), ScopeGroup= ScopeGroupAll, RunStoryAsync = UserStories.TryGetMeAsync });
7172
StoryCollection.Add(new StoryDefinition() { GroupName = usersGroupName, Title = ResourceLoader.GetForCurrentView().GetString("ReadUsers"), ScopeGroup = ScopeGroupAll, RunStoryAsync = UserStories.TryGetUsersAsync });
7273
StoryCollection.Add(new StoryDefinition() { GroupName = usersGroupName, Title = ResourceLoader.GetForCurrentView().GetString("GetDrive"), ScopeGroup = ScopeGroupAll, RunStoryAsync = UserStories.TryGetCurrentUserDriveAsync });

Microsoft-Graph-Snippets-SDK/Microsoft-Graph-Snippets-SDK.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090
</PropertyGroup>
9191
<ItemGroup>
9292
<!-- A reference to the entire .Net Framework and Windows SDK are automatically included -->
93+
<Content Include="LargeFileUploadResource.bmp" />
9394
<PRIResource Include="Assets\Resources.resw" />
9495
<Content Include="excelTestResource.xlsx">
9596
<CopyToOutputDirectory>Always</CopyToOutputDirectory>

Microsoft-Graph-Snippets-SDK/Users/UserSnippets.cs

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,61 @@ public class UserSnippets
2121
{
2222
static string ExcelFileId = null;
2323

24+
public static async Task<string> UploadLargeFile()
25+
{
26+
try
27+
{
28+
var graphClient = AuthenticationHelper.GetAuthenticatedClient();
29+
30+
StorageFile file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync("LargeFileUploadResource.bmp");
31+
32+
using (Stream fileStream = (await file.OpenReadAsync()).AsStreamForRead())
33+
{
34+
// Create the upload session. The access token is no longer required as you have session established for the upload.
35+
// POST /v1.0/drive/root:/UploadLargeFile.bmp:/microsoft.graph.createUploadSession
36+
var uploadSession = await graphClient.Drive.Root.ItemWithPath("LargeFileUploadResource.bmp").CreateUploadSession().Request().PostAsync();
37+
38+
var maxChunkSize = 320 * 1024; // 320 KB - Change this to your chunk size. 5MB is the default.
39+
var provider = new ChunkedUploadProvider(uploadSession, graphClient, fileStream, maxChunkSize);
40+
41+
// Setup the chunk request necessities
42+
var chunkRequests = provider.GetUploadChunkRequests();
43+
var readBuffer = new byte[maxChunkSize];
44+
var trackedExceptions = new List<Exception>();
45+
DriveItem itemResult = null;
46+
47+
//upload the chunks
48+
foreach (var request in chunkRequests)
49+
{
50+
// Do your updates here: update progress bar, etc.
51+
// ...
52+
// Send chunk request
53+
var result = await provider.GetChunkRequestResponseAsync(request, readBuffer, trackedExceptions);
54+
55+
if (result.UploadSucceeded)
56+
{
57+
itemResult = result.ItemResponse;
58+
}
59+
}
60+
61+
// Check that upload succeeded
62+
if (itemResult == null)
63+
{
64+
// Retry the upload
65+
// ...
66+
}
67+
68+
return "Success";
69+
}
70+
}
71+
72+
catch (ServiceException e)
73+
{
74+
Debug.WriteLine("We could not upload the file: " + e.Error.Message);
75+
return null;
76+
}
77+
}
78+
2479
// Returns information about the signed-in user
2580
public static async Task<string> GetMeAsync()
2681
{
@@ -29,23 +84,19 @@ public static async Task<string> GetMeAsync()
2984
try
3085
{
3186
var graphClient = AuthenticationHelper.GetAuthenticatedClient();
32-
3387
var currentUserObject = await graphClient.Me.Request().GetAsync();
3488
currentUserName = currentUserObject.DisplayName;
3589

36-
if ( currentUserName != null)
90+
if (currentUserName != null)
3791
{
3892
Debug.WriteLine("Got user: " + currentUserName);
3993
}
40-
4194
}
4295

43-
4496
catch (ServiceException e)
4597
{
4698
Debug.WriteLine("We could not get the current user: " + e.Error.Message);
4799
return null;
48-
49100
}
50101

51102
return currentUserName;

Microsoft-Graph-Snippets-SDK/Users/UserStories.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ class UserStories
1818
private static readonly string DEFAULT_MESSAGE_BODY = "This message was sent from the Microsoft Graph SDK UWP Snippets project";
1919
public static ApplicationDataContainer _settings = ApplicationData.Current.RoamingSettings;
2020

21+
public static async Task<bool> TryUploadLargeFileAsync()
22+
{
23+
var result = await UserSnippets.UploadLargeFile();
24+
25+
return result != null;
26+
}
27+
2128
public static async Task<bool> TryGetMeAsync()
2229
{
2330
var currentUser = await UserSnippets.GetMeAsync();

Microsoft-Graph-Snippets-SDK/project.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"dependencies": {
3-
"Microsoft.Graph": "1.1.1",
3+
"Microsoft.Graph": "1.2.1",
44
"Microsoft.Identity.Client": "1.0.303282006-alpha",
55
"Microsoft.NETCore.UniversalWindowsPlatform": "5.0.0",
66
"Newtonsoft.Json": "9.0.1"

Microsoft-Graph-Snippets-SDK/project.lock.json

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@
3030
"lib/netcore50/Microsoft.CSharp.dll": {}
3131
}
3232
},
33-
"Microsoft.Graph/1.1.1": {
33+
"Microsoft.Graph/1.2.1": {
3434
"type": "package",
3535
"dependencies": {
36-
"Microsoft.Graph.Core": "1.2.1",
36+
"Microsoft.Graph.Core": "1.3.0",
3737
"Newtonsoft.Json": "[6.0.1, 10.0.0)"
3838
},
3939
"compile": {
@@ -43,7 +43,7 @@
4343
"lib/portable45-net45+win8+wpa81/Microsoft.Graph.dll": {}
4444
}
4545
},
46-
"Microsoft.Graph.Core/1.2.1": {
46+
"Microsoft.Graph.Core/1.3.0": {
4747
"type": "package",
4848
"dependencies": {
4949
"Newtonsoft.Json": "[6.0.1, 10.0.0)"
@@ -1959,10 +1959,10 @@
19591959
"lib/netcore50/Microsoft.CSharp.dll": {}
19601960
}
19611961
},
1962-
"Microsoft.Graph/1.1.1": {
1962+
"Microsoft.Graph/1.2.1": {
19631963
"type": "package",
19641964
"dependencies": {
1965-
"Microsoft.Graph.Core": "1.2.1",
1965+
"Microsoft.Graph.Core": "1.3.0",
19661966
"Newtonsoft.Json": "[6.0.1, 10.0.0)"
19671967
},
19681968
"compile": {
@@ -1972,7 +1972,7 @@
19721972
"lib/portable45-net45+win8+wpa81/Microsoft.Graph.dll": {}
19731973
}
19741974
},
1975-
"Microsoft.Graph.Core/1.2.1": {
1975+
"Microsoft.Graph.Core/1.3.0": {
19761976
"type": "package",
19771977
"dependencies": {
19781978
"Newtonsoft.Json": "[6.0.1, 10.0.0)"
@@ -3745,10 +3745,10 @@
37453745
"lib/netcore50/Microsoft.CSharp.dll": {}
37463746
}
37473747
},
3748-
"Microsoft.Graph/1.1.1": {
3748+
"Microsoft.Graph/1.2.1": {
37493749
"type": "package",
37503750
"dependencies": {
3751-
"Microsoft.Graph.Core": "1.2.1",
3751+
"Microsoft.Graph.Core": "1.3.0",
37523752
"Newtonsoft.Json": "[6.0.1, 10.0.0)"
37533753
},
37543754
"compile": {
@@ -3758,7 +3758,7 @@
37583758
"lib/portable45-net45+win8+wpa81/Microsoft.Graph.dll": {}
37593759
}
37603760
},
3761-
"Microsoft.Graph.Core/1.2.1": {
3761+
"Microsoft.Graph.Core/1.3.0": {
37623762
"type": "package",
37633763
"dependencies": {
37643764
"Newtonsoft.Json": "[6.0.1, 10.0.0)"
@@ -5494,10 +5494,10 @@
54945494
"lib/netcore50/Microsoft.CSharp.dll": {}
54955495
}
54965496
},
5497-
"Microsoft.Graph/1.1.1": {
5497+
"Microsoft.Graph/1.2.1": {
54985498
"type": "package",
54995499
"dependencies": {
5500-
"Microsoft.Graph.Core": "1.2.1",
5500+
"Microsoft.Graph.Core": "1.3.0",
55015501
"Newtonsoft.Json": "[6.0.1, 10.0.0)"
55025502
},
55035503
"compile": {
@@ -5507,7 +5507,7 @@
55075507
"lib/portable45-net45+win8+wpa81/Microsoft.Graph.dll": {}
55085508
}
55095509
},
5510-
"Microsoft.Graph.Core/1.2.1": {
5510+
"Microsoft.Graph.Core/1.3.0": {
55115511
"type": "package",
55125512
"dependencies": {
55135513
"Newtonsoft.Json": "[6.0.1, 10.0.0)"
@@ -7287,10 +7287,10 @@
72877287
"lib/netcore50/Microsoft.CSharp.dll": {}
72887288
}
72897289
},
7290-
"Microsoft.Graph/1.1.1": {
7290+
"Microsoft.Graph/1.2.1": {
72917291
"type": "package",
72927292
"dependencies": {
7293-
"Microsoft.Graph.Core": "1.2.1",
7293+
"Microsoft.Graph.Core": "1.3.0",
72947294
"Newtonsoft.Json": "[6.0.1, 10.0.0)"
72957295
},
72967296
"compile": {
@@ -7300,7 +7300,7 @@
73007300
"lib/portable45-net45+win8+wpa81/Microsoft.Graph.dll": {}
73017301
}
73027302
},
7303-
"Microsoft.Graph.Core/1.2.1": {
7303+
"Microsoft.Graph.Core/1.3.0": {
73047304
"type": "package",
73057305
"dependencies": {
73067306
"Newtonsoft.Json": "[6.0.1, 10.0.0)"
@@ -9036,10 +9036,10 @@
90369036
"lib/netcore50/Microsoft.CSharp.dll": {}
90379037
}
90389038
},
9039-
"Microsoft.Graph/1.1.1": {
9039+
"Microsoft.Graph/1.2.1": {
90409040
"type": "package",
90419041
"dependencies": {
9042-
"Microsoft.Graph.Core": "1.2.1",
9042+
"Microsoft.Graph.Core": "1.3.0",
90439043
"Newtonsoft.Json": "[6.0.1, 10.0.0)"
90449044
},
90459045
"compile": {
@@ -9049,7 +9049,7 @@
90499049
"lib/portable45-net45+win8+wpa81/Microsoft.Graph.dll": {}
90509050
}
90519051
},
9052-
"Microsoft.Graph.Core/1.2.1": {
9052+
"Microsoft.Graph.Core/1.3.0": {
90539053
"type": "package",
90549054
"dependencies": {
90559055
"Newtonsoft.Json": "[6.0.1, 10.0.0)"
@@ -10829,10 +10829,10 @@
1082910829
"lib/netcore50/Microsoft.CSharp.dll": {}
1083010830
}
1083110831
},
10832-
"Microsoft.Graph/1.1.1": {
10832+
"Microsoft.Graph/1.2.1": {
1083310833
"type": "package",
1083410834
"dependencies": {
10835-
"Microsoft.Graph.Core": "1.2.1",
10835+
"Microsoft.Graph.Core": "1.3.0",
1083610836
"Newtonsoft.Json": "[6.0.1, 10.0.0)"
1083710837
},
1083810838
"compile": {
@@ -10842,7 +10842,7 @@
1084210842
"lib/portable45-net45+win8+wpa81/Microsoft.Graph.dll": {}
1084310843
}
1084410844
},
10845-
"Microsoft.Graph.Core/1.2.1": {
10845+
"Microsoft.Graph.Core/1.3.0": {
1084610846
"type": "package",
1084710847
"dependencies": {
1084810848
"Newtonsoft.Json": "[6.0.1, 10.0.0)"
@@ -12593,11 +12593,12 @@
1259312593
"ref/xamarinmac20/_._"
1259412594
]
1259512595
},
12596-
"Microsoft.Graph/1.1.1": {
12597-
"sha512": "AhVlAzDTo8qikv5kjpveYj8N+B6gWgcGJcfba5mJDDqJsE4GdzfybbD8FzXR075AvKyIhTKLPsGjPPevySLCJg==",
12596+
"Microsoft.Graph/1.2.1": {
12597+
"sha512": "ibq4XXSsiMNCWXhXIJX7LKEKqMY1b1dyn8ZGfIlPIm8PVlAh3hiVwphK4lYDlHeLMjCey6b+0Rm3EIsyV47mrg==",
1259812598
"type": "package",
12599+
"path": "Microsoft.Graph/1.2.1",
1259912600
"files": [
12600-
"Microsoft.Graph.1.1.1.nupkg.sha512",
12601+
"Microsoft.Graph.1.2.1.nupkg.sha512",
1260112602
"Microsoft.Graph.nuspec",
1260212603
"lib/MonoAndroid10/Microsoft.Graph.dll",
1260312604
"lib/MonoTouch10/Microsoft.Graph.dll",
@@ -12606,11 +12607,12 @@
1260612607
"lib/portable45-net45+win8+wpa81/Microsoft.Graph.dll"
1260712608
]
1260812609
},
12609-
"Microsoft.Graph.Core/1.2.1": {
12610-
"sha512": "ZVfBio42uIrKT7P2AmUDb5FQiY27hayaeJ1aSj3eocRojkqs15dcjk6+iuVb2qxgK2Re3raJHE31BZUK/+mB/A==",
12610+
"Microsoft.Graph.Core/1.3.0": {
12611+
"sha512": "vGGZXb0P8zx6AVmcRYHixB+yLH+KOVxDhr54OmJtCBTjtmQeVYSGYfkC59hMcPeY2DL7yq/bmvUB/tYHTs6TqA==",
1261112612
"type": "package",
12613+
"path": "Microsoft.Graph.Core/1.3.0",
1261212614
"files": [
12613-
"Microsoft.Graph.Core.1.2.1.nupkg.sha512",
12615+
"Microsoft.Graph.Core.1.3.0.nupkg.sha512",
1261412616
"Microsoft.Graph.Core.nuspec",
1261512617
"lib/MonoAndroid10/Microsoft.Graph.Core.dll",
1261612618
"lib/MonoTouch10/Microsoft.Graph.Core.dll",
@@ -12766,13 +12768,12 @@
1276612768
]
1276712769
},
1276812770
"Microsoft.NETCore.Runtime.CoreCLR-arm/1.0.0": {
12769-
"sha512": "hoJfIl981eXwn9Tz8onO/J1xaYApIfp/YrhjSh9rRhml1U5Wj80LBgyp/6n+KI3VlvcAraThhnHnCTp+M3Uh+w==",
12770-
"type": "Package",
12771+
"sha512": "vUQyaKbHCa7BJAAzdfCP2FfBYOSt0YnDw7VMJLmD1/k68HIJgw1QO7GAfHhqoa39zkkvimC47QBH27wG4C5OGQ==",
12772+
"type": "package",
12773+
"path": "Microsoft.NETCore.Runtime.CoreCLR-arm/1.0.0",
1277112774
"files": [
12775+
"Microsoft.NETCore.Runtime.CoreCLR-arm.1.0.0.nupkg.sha512",
1277212776
"Microsoft.NETCore.Runtime.CoreCLR-arm.nuspec",
12773-
"[Content_Types].xml",
12774-
"_rels/.rels",
12775-
"package/services/metadata/core-properties/c1cbeaed81514106b6b7971ac193f132.psmdcp",
1277612777
"ref/dotnet/_._",
1277712778
"runtimes/win8-arm/lib/dotnet/mscorlib.ni.dll",
1277812779
"runtimes/win8-arm/native/clretwrc.dll",
@@ -15984,11 +15985,13 @@
1598415985
},
1598515986
"projectFileDependencyGroups": {
1598615987
"": [
15987-
"Microsoft.Graph >= 1.1.1",
15988+
"Microsoft.Graph >= 1.2.1",
1598815989
"Microsoft.Identity.Client >= 1.0.303282006-alpha",
1598915990
"Microsoft.NETCore.UniversalWindowsPlatform >= 5.0.0",
1599015991
"Newtonsoft.Json >= 9.0.1"
1599115992
],
1599215993
"UAP,Version=v10.0": []
15993-
}
15994+
},
15995+
"tools": {},
15996+
"projectFileToolGroups": {}
1599415997
}

0 commit comments

Comments
 (0)