private static async Task <bool> RemoveGroupOwner(string grpName, string userName) { try { // get object id for userPrincipalName var userObjectId = GetUserObjectId(userName).GetAwaiter().GetResult(); // get object id fro group name var groupObjectId = GetGroupObjectId(grpName).GetAwaiter().GetResult(); AuthenticationConfig config = AuthenticationConfig.ReadFromJsonFile("appsettings.json"); // call MS Graph Api function that attaches a member to the group var httpClient = new HttpClient(); var apiCaller = new ProtectedApiCallHelper(httpClient); await apiCaller.DeleteWebApiAndProcessResultASync($"{config.ApiUrl}v1.0/groups/{groupObjectId}/owners/{userObjectId}/$ref", Program.AuthenticationResult.AccessToken); return(true); } catch (Exception ex) { return(false); } }
private static async Task CreateGroup(string jsonGroup) { AuthenticationConfig config = AuthenticationConfig.ReadFromJsonFile("appsettings.json"); Debug.WriteLine("===================== Create Alvianda group: ======================="); try { var httpClient = new HttpClient(); var apiCaller = new ProtectedApiCallHelper(httpClient); await apiCaller.PostWebApiAndProcessResultASync($"{config.ApiUrl}v1.0/groups", jsonGroup, Program.AuthenticationResult.AccessToken, Display); } catch (Exception ex) { Debug.WriteLine(ex.Message); } finally { Debug.WriteLine("========================= End list =========================================="); } }
public static async Task <string> GetGroupObjectId(string groupname) { AuthenticationConfig config = AuthenticationConfig.ReadFromJsonFile("appsettings.json"); try { var httpClient = new HttpClient(); var apiCaller = new ProtectedApiCallHelper(httpClient); var result = await apiCaller.GetWebApiAndReturnResultAsync($"{config.ApiUrl}v1.0/groups", Program.AuthenticationResult.AccessToken, Display); // TODO get the group is based on group name (query json result) string groupId = string.Empty; foreach (var group in JObject.Parse(result)["value"].ToList()) { if (group["displayName"].ToString() == groupname) { groupId = group["id"].ToString(); break; } } return(groupId); } catch (Exception ex) { Debug.WriteLine(ex.Message); return(null); } }
private static async Task <bool> AddGroupMember(string grpName, string userName) { try { // get object id for userPrincipalName var userObjectId = GetUserObjectId(userName).GetAwaiter().GetResult(); // get object id fro group name var groupObjectId = GetGroupObjectId(grpName).GetAwaiter().GetResult(); AuthenticationConfig config = AuthenticationConfig.ReadFromJsonFile("appsettings.json"); // call MS Graph Api function that attaches a member to the group var httpClient = new HttpClient(); var apiCaller = new ProtectedApiCallHelper(httpClient); var payloadUrl = "{'@odata.id': 'https://graph.microsoft.com/v1.0/directoryObjects/" + userObjectId + "'}"; await apiCaller.PostWebApiAndProcessResultASync($"{config.ApiUrl}v1.0/groups/{groupObjectId}/members/$ref", payloadUrl, Program.AuthenticationResult.AccessToken, null); return(true); } catch (Exception ex) { return(false); } }
private static async Task DeleteGroup(string groupName) { AuthenticationConfig config = AuthenticationConfig.ReadFromJsonFile("appsettings.json"); try { var groupId = MsGraphFacade.GetGroupObjectId(groupName).GetAwaiter().GetResult(); var httpClient = new HttpClient(); var apiCaller = new ProtectedApiCallHelper(httpClient); await apiCaller.DeleteWebApiAndProcessResultASync($"{config.ApiUrl}v1.0/groups/{groupId}", Program.AuthenticationResult.AccessToken); } catch (Exception ex) { Debug.WriteLine(ex.Message); } }
public static async Task <string> GetUserObjectId(string username) { AuthenticationConfig config = AuthenticationConfig.ReadFromJsonFile("appsettings.json"); try { var httpClient = new HttpClient(); var apiCaller = new ProtectedApiCallHelper(httpClient); var result = await apiCaller.GetWebApiAndReturnResultAsync($"{config.ApiUrl}v1.0/users/{username}", Program.AuthenticationResult.AccessToken, Display); // TODO get the group is based on group name (query json result) string userId = JObject.Parse(result)["id"].ToString(); return(userId); } catch (Exception ex) { Debug.WriteLine(ex.Message); return(null); } }