Example #1
0
        // Create a sharing link for the file if one doesn't already exist.
        // See `https://developer.microsoft.com/en-us/graph/docs/api-reference/v1.0/api/item_createlink`.
        public async Task <string> CreateSharingLinkForFile(string accessToken, OfficeHoursFileInfo file)
        {
            string endpoint = $"https://graph.microsoft.com/v1.0/me/drive/items/{ file.Id }/createLink";
            OfficeHoursSharingLinkInfo link = new OfficeHoursSharingLinkInfo("view");

            using (var client = new HttpClient())
            {
                using (var request = new HttpRequestMessage(HttpMethod.Post, endpoint))
                {
                    //request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                    request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);

                    request.Content = new StringContent(JsonConvert.SerializeObject(link), Encoding.UTF8, "application/json");
                    using (var response = await client.SendAsync(request))
                    {
                        if (response.IsSuccessStatusCode)
                        {
                            string stringResult = await response.Content.ReadAsStringAsync();

                            OfficeHoursPermissionInfo permission = JsonConvert.DeserializeObject <OfficeHoursPermissionInfo>(stringResult);
                            return(permission.Link.WebUrl);
                        }
                        else
                        {
                            return("");
                        }
                    }
                }
            }
        }
Example #2
0
        // Create the email message.
        public async Task <OfficeHoursMessageRequest> BuildEmailMessage(string accessToken, string recipients, string subject)
        {
            // Prepare the recipient list.
            string[] splitter = { ";" };
            string[] splitRecipientsString            = recipients.Split(splitter, StringSplitOptions.RemoveEmptyEntries);
            List <OfficeHoursRecipient> recipientList = new List <OfficeHoursRecipient>();

            foreach (string recipient in splitRecipientsString)
            {
                recipientList.Add(new OfficeHoursRecipient
                {
                    EmailAddress = new OfficeHoursUserInfo
                    {
                        Address = recipient.Trim()
                    }
                });
            }

            // Get the current user's profile photo (or a test image if no profile photo exists).
            using (Stream photo = await GetMyProfilePhoto(accessToken))
            {
                // Add the photo as a file attachment for the email message.
                byte[] photoBytes = null;
                using (var memoryStream = new MemoryStream())
                {
                    photo?.CopyTo(memoryStream);
                    photoBytes = memoryStream.ToArray();
                }
                List <OfficeHoursAttachment> attachments = new List <OfficeHoursAttachment>();
                attachments.Add(new OfficeHoursAttachment
                {
                    ODataType    = "#microsoft.graph.fileAttachment",
                    ContentBytes = photoBytes,
                    Name         = "mypic.jpg"
                });

                // Upload the photo to the user's root drive and then create a sharing link.
                OfficeHoursFileInfo file = await UploadFile(accessToken, photo);

                file.SharingLink = await CreateSharingLinkForFile(accessToken, file);

                // Add the sharing link to the email body.
                string bodyContent = string.Format("Mail Body", file.SharingLink);

                // Build the email message.
                OfficeHoursMessage message = new OfficeHoursMessage
                {
                    Body = new OfficeHoursItemBody
                    {
                        Content     = bodyContent,
                        ContentType = "HTML"
                    },
                    Subject      = subject,
                    ToRecipients = recipientList,
                    Attachments  = attachments
                };

                return(new OfficeHoursMessageRequest
                {
                    Message = message,
                    SaveToSentItems = true
                });
            }
        }