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 =========================================="); } }
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); } }