private void LoadFolderFromPath(string path = null) { // Update the UI for loading something new ShowWork(true); LoadChildren(new DriveItem[0]); try { OneDriveLib.Browser.ResultItem retItem = Browser.ListFolderFromPath(graphClient, path); Microsoft.Graph.DriveItem parentFolder = retItem.ParentFolder; Microsoft.Graph.DriveItem[] resultsItems = retItem.ChildrenItems; ProcessFolder(parentFolder); string totalMessage = "", strMessage; for (int i = 0; i < resultsItems.Length; i++) { strMessage = String.Format("Name:{0}-----ID:{1}", resultsItems[i].Name, resultsItems[i].Id); if (resultsItems[i].Folder != null) { strMessage = strMessage + String.Format("--FolderChild:{0}", resultsItems[i].Folder.ChildCount); } totalMessage = String.Format("{0}\n{1}", totalMessage, strMessage); } MessageBox.Show(totalMessage); } catch (Exception exception) { PresentServiceException(exception); } ShowWork(false); }
private async Task <bool> TryAddItemToArchiveAsync(ZipArchive archive, Microsoft.Graph.DriveItem item, string baseUrl, string accessToken) { bool failure = false; var zipEntry = archive.CreateEntry(item.Name); zipEntry.LastWriteTime = item.LastModifiedDateTime.Value; using (var entryStream = zipEntry.Open()) { try { var itemContentUrl = ActionHelpers.BuildApiUrl(baseUrl, item.ParentReference.DriveId, item.Id, "content"); var contentStream = await HttpHelper.Default.GetStreamContentForUrlAsync(itemContentUrl, accessToken); await contentStream.CopyToAsync(entryStream); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("Error copying data into file stream: " + ex.ToString()); failure = true; } } if (failure) { zipEntry.Delete(); } return(!failure); }
private static async Task UploadFileToOneDriveForBusiness(GraphServiceClient graphClient) { var newFile = new Microsoft.Graph.DriveItem { File = new Microsoft.Graph.File(), Name = "user-photo-two.png", }; newFile = await graphClient.Me.Drive.Root.Children.Request().AddAsync(newFile); using (FileStream fs = new FileStream(@"..\..\user-photo-two.png", FileMode.Open, FileAccess.Read, FileShare.Read)) { var newFileContent = await graphClient.Me.Drive.Items[newFile.Id].Content.Request().PutAsync <DriveItem>(fs); } Console.WriteLine("Uploaded file with ID: {0}", newFile.Id); }
public async Task <string> UploadSheet(HttpContextBase httpContextBase, string path, string saveName, bool examSheet = false) { // This whole process is returning a strange exception that doesn't seem to actually be doing anything. // File is uploaded properly without corruption and application continues normally. // Exception thrown: 'System.InvalidOperationException' in mscorlib.dll string token = await GetAccessToken(httpContextBase); if (string.IsNullOrEmpty(token)) { return(null); } GraphServiceClient client = new GraphServiceClient( new DelegateAuthenticationProvider( (requestMessage) => { requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token); return(Task.FromResult(0)); })); byte[] data = System.IO.File.ReadAllBytes(path); // Writeable stream from byte array for drive upload Stream stream = new MemoryStream(data); DBManager db = new DBManager(); string dir = ""; if (examSheet) { dir = db.GetUserExamDirectory(user); } else { dir = db.GetUserUploadDirectory(user); } Microsoft.Graph.DriveItem file = client.Me.Drive.Root.ItemWithPath(dir + saveName).Content.Request().PutAsync <DriveItem>(stream).Result; return(file.WebUrl); }
private void MySignIn() { try { graphClient = Browser.SignIn(MsaClientId); } catch (ServiceException exception) { PresentServiceException(exception); } ShowWork(true); LoadChildren(new DriveItem[0]); try { OneDriveLib.Browser.ResultItem resultItem = Browser.ListFolderFromPath(graphClient); Microsoft.Graph.DriveItem rootFolder = resultItem.ParentFolder; Microsoft.Graph.DriveItem[] resultsItems = resultItem.ChildrenItems; ProcessFolder(rootFolder); string totalMessage = "", strMessage; for (int i = 0; i < resultsItems.Length; i++) { strMessage = String.Format("Name:{0}-----ID:{1}", resultsItems[i].Name, resultsItems[i].Id); if (resultsItems[i].Folder != null) { strMessage = strMessage + String.Format("--FolderChild:{0}", resultsItems[i].Folder.ChildCount); } totalMessage = String.Format("{0}\n{1}", totalMessage, strMessage); } MessageBox.Show(totalMessage); UpdateConnectedStateUx(true); } catch (ServiceException exception) { PresentServiceException(exception); } ShowWork(false); }
private static async Task UploadFileToOneDriveForBusiness(GraphServiceClient graphClient) { var newFile = new Microsoft.Graph.DriveItem { File = new Microsoft.Graph.File(), Name = "user-photo-two.png", }; newFile = await graphClient.Me.Drive.Root.Children.Request().AddAsync(newFile); using (FileStream fs = new FileStream(@"..\..\user-photo-two.png", FileMode.Open, FileAccess.Read, FileShare.Read)) { var newFileContent = await graphClient.Me.Drive.Items[newFile.Id].Content.Request().PutAsync<DriveItem>(fs); } Console.WriteLine("Uploaded file with ID: {0}", newFile.Id); }
private static Microsoft.Graph.DriveItem[] ProcessFolder(DriveItem folder, ItemType type = ItemType.All) { if (folder != null) { if (folder.Folder != null && folder.Children.CurrentPage != null) { var items = folder.Children.CurrentPage; int nLength = items.Count, i = 0, nCount = 0; Microsoft.Graph.DriveItem[] CurrentItems = null; if (type == ItemType.All) { CurrentItems = new Microsoft.Graph.DriveItem[nLength]; foreach (var obj in items) { Console.WriteLine(obj.Id); CurrentItems[i++] = obj; } } else if (type == ItemType.File) { nCount = i = 0; foreach (var obj in items) { if (obj.Folder != null) { continue; } nCount++; } CurrentItems = new Microsoft.Graph.DriveItem[nCount]; foreach (var obj in items) { if (obj.Folder != null) { continue; } CurrentItems[i++] = obj; } } else if (type == ItemType.Folder) { nCount = i = 0; foreach (var obj in items) { if (obj.Folder == null) { continue; } nCount++; } CurrentItems = new Microsoft.Graph.DriveItem[nCount]; foreach (var obj in items) { if (obj.Folder == null) { continue; } CurrentItems[i++] = obj; } } return(CurrentItems); } } return(null); }
/// <summary> /// Adds the specified DriveItem to the collection via POST. /// </summary> /// <param name="driveItem">The DriveItem to add.</param> /// <param name="cancellationToken">The <see cref="CancellationToken"/> for the request.</param> /// <returns>The created DriveItem.</returns> public System.Threading.Tasks.Task <DriveItem> AddAsync(DriveItem driveItem, CancellationToken cancellationToken) { this.ContentType = "application/json"; this.Method = "POST"; return(this.SendAsync <DriveItem>(driveItem, cancellationToken)); }
/// <summary> /// Adds the specified DriveItem to the collection via POST. /// </summary> /// <param name="driveItem">The DriveItem to add.</param> /// <returns>The created DriveItem.</returns> public System.Threading.Tasks.Task <DriveItem> AddAsync(DriveItem driveItem) { return(this.AddAsync(driveItem, CancellationToken.None)); }
/// <summary> /// Adds the specified DriveItem to the collection via POST. /// </summary> /// <param name="driveItem">The DriveItem to add.</param> /// <returns>The created DriveItem.</returns> public Task <DriveItem> AddAsync(DriveItem driveItem) { return(this.AddAsync(driveItem, CancellationToken.None)); }
/// <summary> /// Creates the specified DriveItem using POST. /// </summary> /// <param name="driveItemToCreate">The DriveItem to create.</param> /// <returns>The created DriveItem.</returns> public System.Threading.Tasks.Task <DriveItem> CreateAsync(DriveItem driveItemToCreate) { return(this.CreateAsync(driveItemToCreate, CancellationToken.None)); }
/// <summary> /// Initializes any collection properties after deserialization, like next requests for paging. /// </summary> /// <param name="driveItemToInitialize">The <see cref="DriveItem"/> with the collection properties to initialize.</param> private void InitializeCollectionProperties(DriveItem driveItemToInitialize) { if (driveItemToInitialize != null && driveItemToInitialize.AdditionalData != null) { if (driveItemToInitialize.Activities != null && driveItemToInitialize.Activities.CurrentPage != null) { driveItemToInitialize.Activities.AdditionalData = driveItemToInitialize.AdditionalData; object nextPageLink; driveItemToInitialize.AdditionalData.TryGetValue("*****@*****.**", out nextPageLink); var nextPageLinkString = nextPageLink as string; if (!string.IsNullOrEmpty(nextPageLinkString)) { driveItemToInitialize.Activities.InitializeNextPageRequest( this.Client, nextPageLinkString); } } if (driveItemToInitialize.Children != null && driveItemToInitialize.Children.CurrentPage != null) { driveItemToInitialize.Children.AdditionalData = driveItemToInitialize.AdditionalData; object nextPageLink; driveItemToInitialize.AdditionalData.TryGetValue("*****@*****.**", out nextPageLink); var nextPageLinkString = nextPageLink as string; if (!string.IsNullOrEmpty(nextPageLinkString)) { driveItemToInitialize.Children.InitializeNextPageRequest( this.Client, nextPageLinkString); } } if (driveItemToInitialize.Permissions != null && driveItemToInitialize.Permissions.CurrentPage != null) { driveItemToInitialize.Permissions.AdditionalData = driveItemToInitialize.AdditionalData; object nextPageLink; driveItemToInitialize.AdditionalData.TryGetValue("*****@*****.**", out nextPageLink); var nextPageLinkString = nextPageLink as string; if (!string.IsNullOrEmpty(nextPageLinkString)) { driveItemToInitialize.Permissions.InitializeNextPageRequest( this.Client, nextPageLinkString); } } if (driveItemToInitialize.Subscriptions != null && driveItemToInitialize.Subscriptions.CurrentPage != null) { driveItemToInitialize.Subscriptions.AdditionalData = driveItemToInitialize.AdditionalData; object nextPageLink; driveItemToInitialize.AdditionalData.TryGetValue("*****@*****.**", out nextPageLink); var nextPageLinkString = nextPageLink as string; if (!string.IsNullOrEmpty(nextPageLinkString)) { driveItemToInitialize.Subscriptions.InitializeNextPageRequest( this.Client, nextPageLinkString); } } if (driveItemToInitialize.Thumbnails != null && driveItemToInitialize.Thumbnails.CurrentPage != null) { driveItemToInitialize.Thumbnails.AdditionalData = driveItemToInitialize.AdditionalData; object nextPageLink; driveItemToInitialize.AdditionalData.TryGetValue("*****@*****.**", out nextPageLink); var nextPageLinkString = nextPageLink as string; if (!string.IsNullOrEmpty(nextPageLinkString)) { driveItemToInitialize.Thumbnails.InitializeNextPageRequest( this.Client, nextPageLinkString); } } if (driveItemToInitialize.Versions != null && driveItemToInitialize.Versions.CurrentPage != null) { driveItemToInitialize.Versions.AdditionalData = driveItemToInitialize.AdditionalData; object nextPageLink; driveItemToInitialize.AdditionalData.TryGetValue("*****@*****.**", out nextPageLink); var nextPageLinkString = nextPageLink as string; if (!string.IsNullOrEmpty(nextPageLinkString)) { driveItemToInitialize.Versions.InitializeNextPageRequest( this.Client, nextPageLinkString); } } } }