/// <summary> /// This method creates and uploads a file into a parent folder with a unique request /// </summary> /// <param name="driveId">The ID of the target drive</param> /// <param name="parentFolderId">The ID of the parent folder</param> /// <param name="file">The file object</param> /// <param name="content">The binary stream of the file content</param> /// <param name="contentType">The content type of the file</param> /// <returns>The just created and uploaded file object</returns> public static DriveItem UploadFileDirect(String driveId, String parentFolderId, DriveItem file, Stream content, String contentType) { var jsonResponse = MicrosoftGraphHelper.MakePutRequestForString( String.Format("{0}drives/{1}/items/{2}/children/{3}/content", MicrosoftGraphHelper.MicrosoftGraphV1BaseUri, driveId, parentFolderId, file.Name), content, contentType); var uploadedFile = JsonConvert.DeserializeObject<DriveItem>(jsonResponse); return (uploadedFile); }
/// <summary> /// This method creates a new folder in OneDrive for Business /// </summary> /// <param name="driveId">The ID of the target drive</param> /// <param name="parentFolderId">The ID of the parent folder</param> /// <param name="folder">The new folder object to create</param> /// <returns>The just created folder</returns> public static DriveItem CreateFolder(String driveId, String parentFolderId, DriveItem folder) { var jsonResponse = MicrosoftGraphHelper.MakePostRequestForString( String.Format("{0}drives/{1}/items/{2}/children", MicrosoftGraphHelper.MicrosoftGraphV1BaseUri, driveId, parentFolderId), folder, "application/json"); var newFolder = JsonConvert.DeserializeObject<DriveItem>(jsonResponse); return (newFolder); }
/// <summary> /// This method creates and uploads a file into a parent folder /// </summary> /// <param name="driveId">The ID of the target drive</param> /// <param name="parentFolderId">The ID of the parent folder</param> /// <param name="file">The file object</param> /// <param name="content">The binary stream of the file content</param> /// <param name="contentType">The content type of the file</param> /// <returns>The just created and uploaded file object</returns> public static DriveItem UploadFile(String driveId, String parentFolderId, DriveItem file, Stream content, String contentType) { var jsonResponse = MicrosoftGraphHelper.MakePostRequestForString( String.Format("{0}drives/{1}/items/{2}/children", MicrosoftGraphHelper.MicrosoftGraphV1BaseUri, driveId, parentFolderId), file, "application/json"); var uploadedFile = JsonConvert.DeserializeObject<DriveItem>(jsonResponse); try { MicrosoftGraphHelper.MakePutRequest( String.Format("{0}drives/{1}/items/{2}/content", MicrosoftGraphHelper.MicrosoftGraphV1BaseUri, driveId, uploadedFile.Id), content, contentType); } catch (ApplicationException ex) { // For whatever reason we come here ... the upload failed // and we need to delete the just created file FilesHelper.DeleteFile(driveId, uploadedFile.Id); // And then we re-throw the exception throw ex; } return (uploadedFile); }
private void UpdateSampleFile(Drive drive, DriveItem newFile, String filePath) { FilesHelper.RenameFile(drive.Id, newFile.Id, "SP2016-MinRoles.jpg"); Stream memPhoto = getFileContent(filePath); try { if (memPhoto.Length > 0) { String contentType = "image/jpeg"; FilesHelper.UpdateFileContent( drive.Id, newFile.Id, memPhoto, contentType); } } catch (Exception ex) { // Handle the exception } }