/// <summary> /// Get a specified group by the group id. /// </summary> /// <param name="id">The group Id.</param> /// <returns>The specified group. Null if it doesn't existed.</returns> public async Task <Group> GetUserAsync(string id) { if (!string.IsNullOrEmpty(id)) { throw new ArgumentNullException(nameof(id)); } string url = ConstructUrl(MICROSOFT_GRAPH_RESOURCE_URI, $"{URL_SUFFIX}/{id}", null); Group group = null; string exception = string.Empty; try { string result = await HttpRequestHelper.CallRestApiRequestAsync(url, AccessToken, null, HttpMethod.Get); var data = JObject.Parse(result); if (data != null) { group = JsonConvert.DeserializeObject <Group>(data.ToString()); } else { exception = result; WriteLine(exception); } } catch (Exception e) { exception = $"Message: {e.Message} StackTrace: {e.StackTrace}"; WriteLine(exception); } return(group); }
/// <summary> /// Get a specified user's messages /// </summary> /// <param name="uid">The user Id or userPrincipalName.</param> /// <returns>The message list, null or empty when there is no messages.</returns> public async Task <List <Message> > GetMessagesByUserAsync(string uid) { if (string.IsNullOrEmpty(uid)) { throw new ArgumentNullException(nameof(uid)); } string url = ConstructUrl(MICROSOFT_GRAPH_RESOURCE_URI, $"users/{uid}/messages", null); List <Message> messages = new List <Message>(); string exception = string.Empty; try { string result = await HttpRequestHelper.CallRestApiRequestAsync(url, AccessToken, null, HttpMethod.Get); var data = JObject.Parse(result).SelectToken("value"); if (data != null) { messages = JsonConvert.DeserializeObject <List <Message> >(data.ToString()); } else { exception = result; WriteLine(exception); } } catch (Exception e) { exception = $"Message: {e.Message} StackTrace: {e.StackTrace}"; WriteLine(exception); } return(messages); }
public async Task <List <Group> > GetAllGroupsAsync() { string url = ConstructUrl(MICROSOFT_GRAPH_RESOURCE_URI, URL_SUFFIX, null); List <Group> groups = new List <Group>(); string exception = string.Empty; try { string result = await HttpRequestHelper.CallRestApiRequestAsync(url, AccessToken, null, HttpMethod.Get); var data = JObject.Parse(result).SelectToken("value"); if (data != null) { groups = JsonConvert.DeserializeObject <List <Group> >(data.ToString()); } else { exception = result; WriteLine(exception); } } catch (Exception e) { exception = $"Message: {e.Message} StackTrace: {e.StackTrace}"; WriteLine(exception); } return(groups); }
/// <summary> /// Get a specified user's message under a specified folder /// </summary> /// <param name="uid">The user Id or userPrincipalName.</param> /// <param name="folderId">The mail folder Id.</param> /// <param name="messageId">The message Id.</param> /// <returns>The message, null when the messages is not existed.</returns> public async Task <Message> GetMessageUnderFolderByUserAsync(string uid, string folderId, string messageId) { if (string.IsNullOrEmpty(uid)) { throw new ArgumentNullException(nameof(uid)); } if (string.IsNullOrEmpty(folderId)) { throw new ArgumentNullException(folderId); } if (string.IsNullOrEmpty(messageId)) { throw new ArgumentNullException(nameof(messageId)); } string url = ConstructUrl(MICROSOFT_GRAPH_RESOURCE_URI, $"users/{uid}/mailFolders/{folderId}/messages/{messageId}", null); Message message = null; string exception = string.Empty; try { string result = await HttpRequestHelper.CallRestApiRequestAsync(url, AccessToken, null, HttpMethod.Get); var data = JObject.Parse(result); if (data != null) { message = JsonConvert.DeserializeObject <Message>(data.ToString()); } else { exception = result; WriteLine(exception); } } catch (Exception e) { exception = $"Message: {e.Message} StackTrace: {e.StackTrace}"; WriteLine(exception); } return(message); }