/// <summary> /// Retrieves the picture of a contact, if any /// </summary> /// <param name="contactId">The ID of the contact</param> /// <returns>The picture as a binary Stream</returns> public static Stream GetContactPhoto(String contactId) { Stream result = null; String contentType = "image/png"; try { result = MicrosoftGraphHelper.MakeGetRequestForStream( String.Format("{0}me/contacts/{1}/photo/$value", MicrosoftGraphHelper.MicrosoftGraphV1BaseUri, contactId), contentType); } catch (ApplicationException ex) { HttpException httpException = ex.InnerException as HttpException; if (httpException != null && httpException.GetHttpCode() == 404) { // If 404 -> The contact does not have a picture // Keep NULL value for result result = null; } } return(result); }
/// <summary> /// This method deletes a contact /// </summary> /// <param name="contactId">The ID of the contact to delete</param> public static void DeleteContact(String contactId) { MicrosoftGraphHelper.MakeDeleteRequest( String.Format("{0}me/contacts/{1}", MicrosoftGraphHelper.MicrosoftGraphV1BaseUri, contactId)); }
/// <summary> /// This method retrieves the email messages from a folder in the current user's mailbox /// </summary> /// <param name="folderId">The ID of the target folder, optional</param> /// <param name="startIndex">The startIndex (0 based) of the email messages to retrieve, optional</param> /// <param name="includeAttachments">Defines whether to include attachments or not, optional</param> /// <returns>A page of up to 10 email messages in the folder</returns> public static List <MailMessage> ListMessages(String folderId = null, Int32 startIndex = 0, Boolean includeAttachments = false) { String targetUrl = null; if (!String.IsNullOrEmpty(folderId)) { targetUrl = String.Format("{0}me/mailFolders/{1}/messages?$skip={2}", MicrosoftGraphHelper.MicrosoftGraphV1BaseUri, folderId, startIndex); } else { targetUrl = String.Format("{0}me/messages?$skip={1}", MicrosoftGraphHelper.MicrosoftGraphV1BaseUri, startIndex); } String jsonResponse = MicrosoftGraphHelper.MakeGetRequestForString(targetUrl); var messages = JsonConvert.DeserializeObject <MailMessageList>(jsonResponse); if (includeAttachments) { foreach (var message in messages.Messages.Where(m => m.HasAttachments)) { message.LoadAttachments(); } } return(messages.Messages); }
/// <summary> /// This method sends an email message /// </summary> /// <param name="message"></param> public static void SendMessage(MailMessageToSend message) { MicrosoftGraphHelper.MakePostRequest( String.Format("{0}me/microsoft.graph.sendMail", MicrosoftGraphHelper.MicrosoftGraphV1BaseUri), message, "application/json"); }
/// <summary> /// This method removes a member from a group /// </summary> /// <param name="user">The user to remove from the group</param> /// <param name="groupId">The ID of the target group</param> public static void RemoveMemberFromGroup(User user, String groupId) { MicrosoftGraphHelper.MakeDeleteRequest( String.Format("{0}groups/{1}/members/{2}/$ref", MicrosoftGraphHelper.MicrosoftGraphV1BaseUri, groupId, user.Id)); }
/// <summary> /// This method creates and uploads a file into a parent folder /// </summary> /// <param name="driveId">The ID of the target drive</param> /// <param name="parentFolderId">The ID of the parent folder</param> /// <param name="file">The file object</param> /// <param name="content">The binary stream of the file content</param> /// <param name="contentType">The content type of the file</param> /// <returns>The just created and uploaded file object</returns> public static DriveItem UploadFile(String driveId, String parentFolderId, DriveItem file, Stream content, String contentType) { var jsonResponse = MicrosoftGraphHelper.MakePostRequestForString( String.Format("{0}drives/{1}/items/{2}/children", MicrosoftGraphHelper.MicrosoftGraphV1BaseUri, driveId, parentFolderId), file, "application/json"); var uploadedFile = JsonConvert.DeserializeObject <DriveItem>(jsonResponse); try { MicrosoftGraphHelper.MakePutRequest( String.Format("{0}drives/{1}/items/{2}/content", MicrosoftGraphHelper.MicrosoftGraphV1BaseUri, driveId, uploadedFile.Id), content, contentType); } catch (ApplicationException ex) { // For whatever reason we come here ... the upload failed // and we need to delete the just created file FilesHelper.DeleteFile(driveId, uploadedFile.Id); // And then we re-throw the exception throw ex; } return(uploadedFile); }
/// <summary> /// This method deletes an event from a calendar /// </summary> /// <param name="calendarId">The ID of the calendar</param> /// <param name="eventId">The ID of the event to delete</param> public static void DeleteEvent(String calendarId, String eventId) { MicrosoftGraphHelper.MakeDeleteRequest( String.Format("{0}me/calendars/{1}/events/{2}", MicrosoftGraphHelper.MicrosoftGraphV1BaseUri, calendarId, eventId)); }
/// <summary> /// This method replies to a thread of an Office 365 Group /// </summary> /// <param name="groupId">The ID of the thread</param> /// <param name="threadId">The ID of the thread</param> /// <param name="post">The post to send as the reply</param> public static void ReplyToUnifiedGroupThread(String groupId, String threadId, ConversationThreadPost post) { MicrosoftGraphHelper.MakePostRequest( String.Format("{0}groups/{1}/threads/{2}/reply", MicrosoftGraphHelper.MicrosoftGraphV1BaseUri, groupId, threadId), new { post }, "application/json"); }
/// <summary> /// This method updated an existing user in Azure AD /// </summary> /// <param name="user">The user's fields to update</param> public static void UpdateUser(User user) { MicrosoftGraphHelper.MakePatchRequestForString( String.Format("{0}users/{1}", MicrosoftGraphHelper.MicrosoftGraphV1BaseUri, user.Id), user, "application/json"); }
/// <summary> /// This method removes a permission from a target DriveItem /// </summary> /// <param name="driveItemId">The ID of the DriveItem</param> /// <param name="permissionId">The ID of the permission</param> public static void RemoveDriveItemPermission(String driveItemId, String permissionId) { MicrosoftGraphHelper.MakeDeleteRequest( String.Format("{0}me/drive/items/{1}/permissions/{2}", MicrosoftGraphHelper.MicrosoftGraphV1BaseUri, driveItemId, permissionId)); }
/// <summary> /// This method deletes a file in OneDrive for Business /// </summary> /// <param name="driveId">The ID of the target drive</param> /// <param name="fileId">The ID of the target file</param> public static void DeleteFile(String driveId, String fileId) { MicrosoftGraphHelper.MakeDeleteRequest( String.Format("{0}drives/{1}/items/{2}", MicrosoftGraphHelper.MicrosoftGraphV1BaseUri, driveId, fileId)); }
/// <summary> /// Updates the photo of an Office 365 Group /// </summary> /// <param name="groupId">The ID of the target group</param> /// <param name="photo">The byte array of the photo</param> public static void UpdateUnifiedGroupPhoto(String groupId, Stream photo) { MicrosoftGraphHelper.MakePatchRequestForString( String.Format("{0}groups/{1}/photo/$value", MicrosoftGraphHelper.MicrosoftGraphV1BaseUri, groupId), photo, "image/jpeg"); }
/// <summary> /// This method sends a reply all to an email message /// </summary> /// <param name="messageId">The ID of the message to reply all to</param> /// <param name="comment">Any comment to include in the reply all, optional</param> public static void ReplyAll(String messageId, String comment = null) { MicrosoftGraphHelper.MakePostRequest( String.Format("{0}me/messages/{1}/replyAll", MicrosoftGraphHelper.MicrosoftGraphV1BaseUri, messageId), content: !String.IsNullOrEmpty(comment) ? new { Comment = comment } : null, contentType: "application/json"); }
/// <summary> /// This method returns the root folder of the personal drive of the current user /// </summary> /// <returns>The root folder of the current user's personal drive</returns> public static DriveItem GetUserPersonalDriveRoot() { String jsonResponse = MicrosoftGraphHelper.MakeGetRequestForString( String.Format("{0}me/drive/root", MicrosoftGraphHelper.MicrosoftGraphV1BaseUri)); var folder = JsonConvert.DeserializeObject <DriveItem>(jsonResponse); return(folder); }
/// <summary> /// This method returns the personal drive of the current user /// </summary> /// <returns>The current user's personal drive</returns> public static Drive GetUserPersonalDrive() { String jsonResponse = MicrosoftGraphHelper.MakeGetRequestForString( String.Format("{0}me/drive", MicrosoftGraphHelper.MicrosoftGraphV1BaseUri)); var drive = JsonConvert.DeserializeObject <Drive>(jsonResponse); return(drive); }
/// <summary> /// This method retrieves the list of owners of a group /// </summary> /// <param name="groupId">The ID of the group</param> /// <returns>The owners of the group</returns> public static List <User> ListGroupOwners(String groupId) { String jsonResponse = MicrosoftGraphHelper.MakeGetRequestForString( String.Format("{0}groups/{1}/owners", MicrosoftGraphHelper.MicrosoftGraphV1BaseUri, groupId)); var usersList = JsonConvert.DeserializeObject <UsersList>(jsonResponse); return(usersList.Users); }
/// <summary> /// This method retrieves the list of all the external users for a tenant /// </summary> /// <param name="numberOfItems">Defines the TOP number of items to retrieve</param> /// <returns>The list of externa users in Azure AD</returns> public static List <User> ListExternalUsers(Int32 numberOfItems = 100) { String jsonResponse = MicrosoftGraphHelper.MakeGetRequestForString( String.Format("{0}users?$filter=userType%20eq%20'Guest'&$top={1}", MicrosoftGraphHelper.MicrosoftGraphV1BaseUri, numberOfItems)); var usersList = JsonConvert.DeserializeObject <UsersList>(jsonResponse); return(usersList.Users); }
/// <summary> /// This method retrieves a specific group registered in Azure AD /// </summary> /// <param name="groupId">The ID of the group</param> /// <returns>The group instance</returns> public static Group GetGroup(String groupId) { String jsonResponse = MicrosoftGraphHelper.MakeGetRequestForString( String.Format("{0}groups/{1}", MicrosoftGraphHelper.MicrosoftGraphV1BaseUri, groupId)); var group = JsonConvert.DeserializeObject <Group>(jsonResponse); return(group); }
/// <summary> /// This method retrieves the photo of a group from Azure AD /// </summary> /// <param name="groupId">The ID of the group</param> /// <returns>The group's photo retrieved from Azure AD</returns> public static Stream GetGroupPhoto(String groupId) { String contentType = "image/png"; var result = MicrosoftGraphHelper.MakeGetRequestForStream( String.Format("{0}groups/{1}/photo/$value", MicrosoftGraphHelper.MicrosoftGraphV1BaseUri, groupId), contentType); return(result); }
/// <summary> /// This method retrieves the list of Security Groups /// </summary> /// <param name="numberOfItems">Defines the TOP number of items to retrieve</param> /// <returns>The list of Security Groups</returns> public static List <Group> ListSecurityGroups(Int32 numberOfItems = 100) { String jsonResponse = MicrosoftGraphHelper.MakeGetRequestForString( String.Format("{0}groups?$filter=SecurityEnabled%20eq%20true" + "&$top={1}", MicrosoftGraphHelper.MicrosoftGraphV1BaseUri, numberOfItems)); var groupsList = JsonConvert.DeserializeObject <GroupsList>(jsonResponse); return(groupsList.Groups); }
/// <summary> /// This method retrieves the list of Office 365 Groups /// </summary> /// <param name="numberOfItems">Defines the TOP number of items to retrieve</param> /// <returns>The list of Office 365 Groups</returns> public static List <Group> ListUnifiedGroups(Int32 numberOfItems = 100) { String jsonResponse = MicrosoftGraphHelper.MakeGetRequestForString( String.Format("{0}groups?$filter=groupTypes/any(gt:%20gt%20eq%20'Unified')" + "&$top={1}", MicrosoftGraphHelper.MicrosoftGraphV1BaseUri, numberOfItems)); var groupsList = JsonConvert.DeserializeObject <GroupsList>(jsonResponse); return(groupsList.Groups); }
/// <summary> /// This method returns the groups of a user /// </summary> /// <param name="upn">The UPN of the user</param> /// <returns>The user's groups</returns> public static List <Group> GetUserGroups(String upn) { String jsonResponse = MicrosoftGraphHelper.MakeGetRequestForString( String.Format("{0}users/{1}/memberOf", MicrosoftGraphHelper.MicrosoftGraphV1BaseUri, upn)); var userGroups = JsonConvert.DeserializeObject <GroupsList>(jsonResponse); return(userGroups.Groups); }
/// <summary> /// This method returns the direct reports of a user /// </summary> /// <param name="upn">The UPN of the user</param> /// <returns>The user's direct reports</returns> public static List <User> GetUserDirectReports(String upn) { String jsonResponse = MicrosoftGraphHelper.MakeGetRequestForString( String.Format("{0}users/{1}/directReports", MicrosoftGraphHelper.MicrosoftGraphV1BaseUri, upn)); var directReports = JsonConvert.DeserializeObject <UsersList>(jsonResponse); return(directReports.Users); }
/// <summary> /// Uploads a new file content on top of an already existing file /// </summary> /// <param name="driveId">The ID of the target drive</param> /// <param name="fileId">The ID of the target file</param> /// <param name="content">The binary stream of the file content</param> /// <param name="contentType">The content type of the file</param> public static void UpdateFileContent(String driveId, String fileId, Stream content, String contentType) { MicrosoftGraphHelper.MakePutRequest( String.Format("{0}drives/{1}/items/{2}/content", MicrosoftGraphHelper.MicrosoftGraphV1BaseUri, driveId, fileId), content, contentType); }
/// <summary> /// This method returns the content of a specific file by ID /// </summary> /// <param name="driveId">The ID of the target drive</param> /// <param name="fileId">The ID of the target file</param> /// <returns>The content of the file as a Stream</returns> public static Stream GetFileContent(String driveId, String fileId, String contentType) { Stream fileContent = MicrosoftGraphHelper.MakeGetRequestForStream( String.Format("{0}drives/{1}/items/{2}/content", MicrosoftGraphHelper.MicrosoftGraphV1BaseUri, driveId, fileId), contentType); return(fileContent); }
/// <summary> /// This method retrieves the calendar of an Office 365 Group /// </summary> /// <param name="groupId">The ID of the group</param> /// <returns>The calendar of an Office 365 Group</returns> public static Calendar GetUnifiedGroupCalendar(String groupId) { String jsonResponse = MicrosoftGraphHelper.MakeGetRequestForString( String.Format("{0}groups/{1}/calendar", MicrosoftGraphHelper.MicrosoftGraphV1BaseUri, groupId)); var calendar = JsonConvert.DeserializeObject <Calendar>(jsonResponse); return(calendar); }
/// <summary> /// This method retrieves the email folders of the current user /// </summary> /// <param name="startIndex">The startIndex (0 based) of the folders to retrieve, optional</param> /// <returns>A page of up to 10 email folders</returns> public static List <MailFolder> ListFolders(Int32 startIndex = 0) { String jsonResponse = MicrosoftGraphHelper.MakeGetRequestForString( String.Format("{0}me/mailFolders?$skip={1}", MicrosoftGraphHelper.MicrosoftGraphV1BaseUri, startIndex)); var folders = JsonConvert.DeserializeObject <MailFolderList>(jsonResponse); return(folders.Folders); }
/// <summary> /// This method returns the manager of a user /// </summary> /// <param name="upn">The UPN of the user</param> /// <returns>The user's manager</returns> public static User GetUserManager(String upn) { String jsonResponse = MicrosoftGraphHelper.MakeGetRequestForString( String.Format("{0}users/{1}/manager", MicrosoftGraphHelper.MicrosoftGraphV1BaseUri, upn)); var user = JsonConvert.DeserializeObject <User>(jsonResponse); return(user); }
/// <summary> /// This method adds a new user to Azure AD /// </summary> /// <param name="user">The user to add</param> /// <returns>The just added user</returns> public static User AddUser(User user) { String jsonResponse = MicrosoftGraphHelper.MakePostRequestForString( String.Format("{0}users", MicrosoftGraphHelper.MicrosoftGraphV1BaseUri), user, "application/json"); var addedUser = JsonConvert.DeserializeObject <User>(jsonResponse); return(addedUser); }
/// <summary> /// Creates/Adds a new Office 365 Group /// </summary> /// <param name="group">The group tp add/create</param> /// <returns>The just added group</returns> public static Group AddUnifiedGroup(Group group) { String jsonResponse = MicrosoftGraphHelper.MakePostRequestForString( String.Format("{0}groups", MicrosoftGraphHelper.MicrosoftGraphV1BaseUri), group, "application/json"); var addedGroup = JsonConvert.DeserializeObject <Group>(jsonResponse); return(addedGroup); }