public async Task Upload() { if (ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier) == null) { //not authenticated RedirectToAction("", "Home"); } var length = Request.ContentLength; if (length > 0) { SharePointClient client = await GetSharePointClient(); var fileName = Request.Headers["X-File-Name"]; try { // First check if a file with the same name already exists. If it exists, delete it. var item = await client.Files.GetByPathAsync(fileName); await item.DeleteAsync(); } catch { //there is no file with the same name, swallow the error and continue } var oneDriveFile = new Microsoft.Office365.SharePoint.FileServices.File { Name = fileName }; await client.Files.AddItemAsync(oneDriveFile); await client.Files.GetById(oneDriveFile.Id).ToFile().UploadAsync(Request.InputStream); } else { throw new Exception("Empty piece"); } }