/// <summary> /// Send an email message. /// </summary> /// <param name="content">Email Body.</param> /// <param name="subject">Eamil Subject.</param> /// <returns>Completed Task.</returns> public async Task SendMessageAsync(string content, string subject) { try { var userObject = await ExecuteGraphFetchAsync(graphBaseUrl); List <Recipient> re = new List <Recipient>(); re.Add(new Recipient { EmailAddress = new EmailAddress { Address = userObject["userPrincipalName"], }, }); // Create the message. Message email = new Message { Body = new ItemBody { Content = content, ContentType = BodyType.Html, }, Subject = subject, ToRecipients = re, }; // Send the message. await this.graphServiceClient.Me.SendMail(email, true).Request().PostAsync(); } catch (ServiceException ex) { throw ServiceHelper.HandleGraphAPIException(ex); } }
/// <summary> /// Init task service. /// </summary> /// <param name="token">Task service token.</param> /// <param name="pageIds">OneNote page name and id dictionary.</param> /// <param name="client">the httpclient for making the API request.</param> /// <returns>Task service itself.</returns> public async Task <ITaskService> InitAsync(string token, Dictionary <string, string> pageIds, HttpClient client = null) { try { IsListCreated = pageIds.Count == 0; if (client == null) { httpClient = ServiceHelper.GetHttpClient(token); } else { httpClient = client; } if (!pageIds.ContainsKey(ToDoStrings.ToDo) || !pageIds.ContainsKey(ToDoStrings.Grocery) || !pageIds.ContainsKey(ToDoStrings.Shopping)) { var notebookId = await GetOrCreateNotebookAsync(ToDoStrings.OneNoteBookName); var sectionId = await GetOrCreateSectionAsync(notebookId, ToDoStrings.OneNoteSectionName); if (!pageIds.ContainsKey(ToDoStrings.ToDo)) { var toDoPageId = await GetOrCreatePageAsync(sectionId, ToDoStrings.ToDo); pageIds.Add(ToDoStrings.ToDo, toDoPageId.Item1); IsListCreated = IsListCreated && toDoPageId.Item2; } if (!pageIds.ContainsKey(ToDoStrings.Grocery)) { var groceryPageId = await GetOrCreatePageAsync(sectionId, ToDoStrings.Grocery); pageIds.Add(ToDoStrings.Grocery, groceryPageId.Item1); IsListCreated = IsListCreated && groceryPageId.Item2; } if (!pageIds.ContainsKey(ToDoStrings.Shopping)) { var shoppingPageId = await GetOrCreatePageAsync(sectionId, ToDoStrings.Shopping); pageIds.Add(ToDoStrings.Shopping, shoppingPageId.Item1); IsListCreated = IsListCreated && shoppingPageId.Item2; } } this.pageIds = pageIds; return(this); } catch (ServiceException ex) { throw ServiceHelper.HandleGraphAPIException(ex); } }
/// <summary> /// Delete tasks. /// </summary> /// <param name="listType">Task list type.</param> /// <param name="taskItems">Task items.</param> /// <returns>True if succeed.</returns> public async Task <bool> DeleteTasksAsync(string listType, List <TaskItem> taskItems) { try { var requestUrl = GraphBaseUrl + "tasks"; return(await this.ExecuteTasksDeleteAsync(requestUrl, taskItems)); } catch (ServiceException ex) { throw ServiceHelper.HandleGraphAPIException(ex); } }
/// <summary> /// Add a task. /// </summary> /// <param name="listType">Task list type.</param> /// <param name="taskText">The task text.</param> /// <returns>Ture if succeed.</returns> public async Task <bool> AddTaskAsync(string listType, string taskText) { try { var requestUrl = GraphBaseUrl + "taskFolders/" + taskFolderIds[listType] + "/tasks"; return(await this.ExecuteTaskAddAsync(requestUrl, taskText)); } catch (ServiceException ex) { throw ServiceHelper.HandleGraphAPIException(ex); } }
/// <summary> /// Get To Do tasks. /// </summary> /// <param name="listType">Task list type.</param> /// <returns>List of task items.</returns> public async Task <List <TaskItem> > GetTasksAsync(string listType) { try { var requestUrl = GraphBaseUrl + "taskFolders/" + taskFolderIds[listType] + "/tasks"; return(await this.ExecuteTasksGetAsync(requestUrl)); } catch (ServiceException ex) { throw ServiceHelper.HandleGraphAPIException(ex); } }
/// <summary> /// Get the sender address of current user. /// </summary> /// <returns>The sender address.</returns> public async Task <string> GetSenderMailAddressAsync() { try { var userObject = await ExecuteGraphFetchAsync(graphBaseUrl); var senderMailAddress = userObject["userPrincipalName"]; return(senderMailAddress); } catch (ServiceException ex) { throw ServiceHelper.HandleGraphAPIException(ex); } }
/// <summary> /// Delete tasks. /// </summary> /// <param name="listType">Task list type.</param> /// <param name="taskItems">Task items.</param> /// <returns>True if succeed.</returns> public async Task <bool> DeleteTasksAsync(string listType, List <TaskItem> taskItems) { try { var pageContentUrl = await this.GetDefaultToDoPageAsync(listType); var httpRequestMessage = ServiceHelper.GenerateDeleteToDosHttpRequest(taskItems, pageContentUrl.ContentUrl); var result = await ExecuteSendAsync(httpRequestMessage); return(result); } catch (ServiceException ex) { throw ServiceHelper.HandleGraphAPIException(ex); } }
/// <summary> /// Init task service. /// </summary> /// <param name="token">Task service token.</param> /// <param name="pageIds">OneNote page name and id dictionary.</param> /// <param name="client">the httpclient for making the API request.</param> /// <returns>Task service itself.</returns> public async Task <ITaskService> InitAsync(string token, Dictionary <string, string> pageIds, HttpClient client = null) { try { IsListCreated = pageIds.Count == 0; if (client == null) { httpClient = ServiceHelper.GetHttpClient(token); } else { httpClient = client; } List <string> customizedListTypes = ServiceHelper.GetListTypes(this.Configuration); string notebookId = null; string sectionId = null; foreach (var customizedListType in customizedListTypes) { string pageTitle = customizedListType; if (!pageIds.ContainsKey(pageTitle)) { if (notebookId == null || sectionId == null) { notebookId = await GetOrCreateNotebookAsync(ToDoStrings.OneNoteBookName); sectionId = await GetOrCreateSectionAsync(notebookId, ToDoStrings.OneNoteSectionName); } var pageId = await GetOrCreatePageAsync(sectionId, pageTitle); pageIds.Add(pageTitle, pageId.Item1); IsListCreated = IsListCreated && pageId.Item2; } } this.pageIds = pageIds; return(this); } catch (ServiceException ex) { throw ServiceHelper.HandleGraphAPIException(ex); } }
/// <summary> /// Add a task. /// </summary> /// <param name="listType">Task list type.</param> /// <param name="taskText">The task text.</param> /// <returns>Ture if succeed.</returns> public async Task <bool> AddTaskAsync(string listType, string taskText) { try { var pageContentUrl = await this.GetDefaultToDoPageAsync(listType); var todoContent = await ExecuteContentFetchAsync(pageContentUrl.ContentUrl + "/?includeIDs=true"); var httpRequestMessage = ServiceHelper.GenerateAddToDoHttpRequest(taskText, todoContent, pageContentUrl.ContentUrl); var result = await ExecuteSendAsync(httpRequestMessage); return(result); } catch (ServiceException ex) { throw ServiceHelper.HandleGraphAPIException(ex); } }
/// <summary> /// Initializes Outlook task service using token. /// </summary> /// <param name="token">The token used for msgraph API call.</param> /// <param name="taskFolderIds">Task folder ids.</param> /// <param name="client">The http client to call API.</param> /// <returns>Outlook task service itself.</returns> public async Task <ITaskService> InitAsync(string token, Dictionary <string, string> taskFolderIds, HttpClient client = null) { try { IsListCreated = taskFolderIds.Count == 0; if (client == null) { this.httpClient = ServiceHelper.GetHttpClient(token); } else { this.httpClient = client; } List <string> customizedListTypes = ServiceHelper.GetListTypes(this.Configuration); foreach (string customizedListType in customizedListTypes) { string taskFolderName = customizedListType; if (!taskFolderIds.ContainsKey(taskFolderName)) { var taskFolderId = await GetOrCreateTaskFolderAsync(taskFolderName); taskFolderIds.Add(taskFolderName, taskFolderId.Item1); IsListCreated = IsListCreated && taskFolderId.Item2; } else { // This is used for the scenario of task folder being deleted. // If a task folder is deleted, will create a new one. var taskFolderId = await GetOrCreateTaskFolderByIdAsync(taskFolderIds[taskFolderName], taskFolderName); taskFolderIds[taskFolderName] = taskFolderId; } } this.taskFolderIds = taskFolderIds; return(this); } catch (ServiceException ex) { throw ServiceHelper.HandleGraphAPIException(ex); } }
/// <summary> /// Get To Do tasks. /// </summary> /// <param name="listType">Task list type.</param> /// <returns>List of task items.</returns> public async Task <List <TaskItem> > GetTasksAsync(string listType) { try { var pages = await GetOneNotePageByIdAsync(pageIds[listType]); var retryCount = 2; while ((pages == null || pages.Count == 0) && retryCount > 0) { pages = await GetOneNotePageByIdAsync(pageIds[listType]); retryCount--; } var todos = await GetToDoContentAsync(pages.First().ContentUrl); return(todos); } catch (ServiceException ex) { throw ServiceHelper.HandleGraphAPIException(ex); } }
/// <summary> /// Initializes Outlook task service using token. /// </summary> /// <param name="token">The token used for msgraph API call.</param> /// <param name="taskFolderIds">Task folder ids.</param> /// <param name="client">The http client to call API.</param> /// <returns>Outlook task service itself.</returns> public async Task <ITaskService> InitAsync(string token, Dictionary <string, string> taskFolderIds, HttpClient client = null) { try { IsListCreated = taskFolderIds.Count == 0; if (client == null) { this.httpClient = ServiceHelper.GetHttpClient(token); } else { this.httpClient = client; } if (!taskFolderIds.ContainsKey(ToDoStrings.ToDo)) { var taskFolderId = await GetOrCreateTaskFolderAsync(ToDoStrings.ToDo); taskFolderIds.Add(ToDoStrings.ToDo, taskFolderId.Item1); IsListCreated = IsListCreated && taskFolderId.Item2; } else { // This is used for the scenario of task folder being deleted. // If a task folder is deleted, will create a new one. var taskFolderId = await GetOrCreateTaskFolderByIdAsync(taskFolderIds[ToDoStrings.ToDo], ToDoStrings.ToDo); taskFolderIds[ToDoStrings.ToDo] = taskFolderId; } if (!taskFolderIds.ContainsKey(ToDoStrings.Grocery)) { var taskFolderId = await GetOrCreateTaskFolderAsync(ToDoStrings.Grocery); taskFolderIds.Add(ToDoStrings.Grocery, taskFolderId.Item1); IsListCreated = IsListCreated && taskFolderId.Item2; } else { var taskFolderId = await GetOrCreateTaskFolderByIdAsync(taskFolderIds[ToDoStrings.Grocery], ToDoStrings.Grocery); taskFolderIds[ToDoStrings.Grocery] = taskFolderId; } if (!taskFolderIds.ContainsKey(ToDoStrings.Shopping)) { var taskFolderId = await GetOrCreateTaskFolderAsync(ToDoStrings.Shopping); taskFolderIds.Add(ToDoStrings.Shopping, taskFolderId.Item1); IsListCreated = IsListCreated && taskFolderId.Item2; } else { var taskFolderId = await GetOrCreateTaskFolderByIdAsync(taskFolderIds[ToDoStrings.Shopping], ToDoStrings.Shopping); taskFolderIds[ToDoStrings.Shopping] = taskFolderId; } this.taskFolderIds = taskFolderIds; return(this); } catch (ServiceException ex) { throw ServiceHelper.HandleGraphAPIException(ex); } }