public async Task <HttpResponseMessage> DownloadFile(string userUpn, string fileId) { var accessTokens = ReadAccessTokenFromCookie(); var sharePointResourceUri = await GetSharePointPersonalSiteResourceUrlAsync(userUpn, accessTokens); var accessToken = await accessTokens.GetAccessTokenForResourceAsync(sharePointResourceUri, false); // Workaround: Retrieve the webUrl for the file being requested string requestUrl = string.Format("{0}/_api/v2.0/drives/{1}/items/{2}", sharePointResourceUri, userUpn, fileId); // Grab the webUrl property from the file Models.DriveItem fileMetadata = await GetResponse <Models.DriveItem>(requestUrl, accessToken); var webUrl = new UriBuilder(fileMetadata.WebUrl); // Download the file using the SharePoint 2013 REST API var relativePath = webUrl.Path; string downloadUrl = string.Format("{0}/_api/web/GetFileByServerRelativeUrl('{1}')/$value", accessTokens.SharePointMySiteUri, relativePath); var contentStream = await GetResponseStreamAsync(downloadUrl, accessToken); HttpResponseMessage message = new HttpResponseMessage(HttpStatusCode.OK); message.Content = new StreamContent(contentStream); message.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); message.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = fileMetadata.Name }; return(message); }
private Models.DriveItem UploadSampleFile(Models.Drive drive, Models.DriveItem newFolder, String filePath) { Models.DriveItem result = null; Stream memPhoto = getFileContent(filePath); try { if (memPhoto.Length > 0) { String contentType = "image/png"; result = FilesHelper.UploadFile(drive.Id, newFolder.Id, new Models.DriveItem { File = new Models.File { }, Name = filePath.Substring(filePath.LastIndexOf("\\") + 1), ConflictBehavior = "rename", }, memPhoto, contentType); } } catch (Exception ex) { // Handle the exception } return(result); }
public async Task <ActionResult> Details(string id, string userId) { // Initialize the GraphServiceClient. var graphClient = _graphSdkHelper.GetAuthenticatedClient(userId); GroupModel details = null; try { var group = await GraphService.GetGroupDetailsAsync(graphClient, id); var pic = await GraphService.GetGroupPictureBase64(graphClient, id); details = new GroupModel { Id = group.Id, Classification = group.Classification, CreatedDateTime = group.CreatedDateTime ?? null, RenewedDateTime = group.RenewedDateTime ?? null, Description = group.Description, GroupType = String.Join(' ', group.GroupTypes), Mail = group.Mail, Name = group.DisplayName, Visibility = group.Visibility, Thumbnail = pic }; if (details.GroupType == "Unified") { var policies = await GraphService.GetGroupPolicyAsync(graphClient, id); var policy = policies.FirstOrDefault(); if (policy != null) { details.Policy = $"{policy.GroupLifetimeInDays} Day expiration"; } var drive = await GraphService.GetGroupDriveAsync(graphClient, id); details.DriveWebUrl = drive.WebUrl; var driveItems = await GraphService.GetDriveRecentItemsAsync(graphClient, drive.Id); if (driveItems.Count == 1) { var graphDriveItem = driveItems[0]; var thumbnailUrl = await GraphService.GetDriveItemThumbnail(graphClient, drive.Id, graphDriveItem.Id); var driveItem = new Models.DriveItem(graphDriveItem); driveItem.ThumbnailUrl = thumbnailUrl; details.DriveRecentItems.Add(driveItem); } if (driveItems.Count > 1) { foreach (var item in driveItems) { details.DriveRecentItems.Add(new Models.DriveItem(item)); } } var convo = await GraphService.GetGroupLatestConversationAsync(graphClient, id); details.LatestConversation = new Models.Conversation { Topic = convo.Topic, LastDeliveredDateTime = convo.LastDeliveredDateTime, }; details.LatestConversation.UniqueSenders.AddRange(convo.UniqueSenders); } details.InfoCard = CreateGroupCard(details); } catch (ServiceException e) { return(Json(new { Message = "An unknown error has occurred." })); } System.Diagnostics.Debug.WriteLine(_msalLog.GetLog()); return(Json(details)); }