protected override void ExecuteCmdlet() { var existingTask = PlannerUtility.GetTaskAsync(HttpClient, AccessToken, TaskId, false, false).GetAwaiter().GetResult(); if (existingTask != null) { var plannerTask = new PlannerTask(); if (ParameterSpecified(nameof(Title))) { plannerTask.Title = Title; } if (ParameterSpecified(nameof(Bucket))) { var bucket = Bucket.GetBucket(HttpClient, AccessToken, existingTask.PlanId); if (bucket != null) { plannerTask.BucketId = bucket.Id; } } if (ParameterSpecified(nameof(PercentComplete))) { plannerTask.PercentComplete = PercentComplete; } if (ParameterSpecified(nameof(DueDateTime))) { plannerTask.DueDateTime = DueDateTime.ToUniversalTime(); } if (ParameterSpecified(nameof(StartDateTime))) { plannerTask.StartDateTime = StartDateTime.ToUniversalTime(); } if (ParameterSpecified(nameof(AssignedTo))) { plannerTask.Assignments = new System.Collections.Generic.Dictionary <string, TaskAssignment>(); var chunks = BatchUtility.Chunk(AssignedTo, 20); foreach (var chunk in chunks) { var userIds = BatchUtility.GetPropertyBatchedAsync(HttpClient, AccessToken, chunk.ToArray(), "/users/{0}", "id").GetAwaiter().GetResult(); foreach (var userId in userIds) { plannerTask.Assignments.Add(userId.Value, new TaskAssignment()); } } foreach (var existingAssignment in existingTask.Assignments) { if (plannerTask.Assignments.FirstOrDefault(t => t.Key == existingAssignment.Key).Key == null) { plannerTask.Assignments.Add(existingAssignment.Key, null); } } } PlannerUtility.UpdateTaskAsync(HttpClient, AccessToken, existingTask, plannerTask).GetAwaiter().GetResult(); } else { throw new PSArgumentException("Task not found", nameof(TaskId)); } }
public static async Task <PlannerTask> AddTaskAsync(HttpClient httpClient, string accessToken, string planId, string bucketId, string title, string[] assignedTo = null) { StringContent stringContent = null; if (assignedTo != null) { var assignments = new Dictionary <string, object>(); var chunks = BatchUtility.Chunk(assignedTo, 20); foreach (var chunk in chunks) { var results = await BatchUtility.GetPropertyBatchedAsync(httpClient, accessToken, chunk.ToArray(), "/users/{0}", "id"); foreach (var userid in results.Select(r => r.Value)) { assignments.Add(userid, new Model.Planner.PlannerAssignedToUser()); } } stringContent = new StringContent(JsonSerializer.Serialize(new { planId = planId, bucketId = bucketId, title = title, assignments = assignments })); } else { stringContent = new StringContent(JsonSerializer.Serialize(new { planId = planId, bucketId = bucketId, title = title })); } stringContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); return(await GraphHelper.PostAsync <PlannerTask>(httpClient, "v1.0/planner/tasks", stringContent, accessToken)); }
internal static async Task <IEnumerable <Microsoft365Group> > GetGroupsAsync(HttpClient httpClient, string accessToken, bool includeSiteUrl, bool includeOwners) { var items = new List <Microsoft365Group>(); var result = await GraphHelper.GetAsync <RestResultCollection <Microsoft365Group> >(httpClient, "v1.0/groups", accessToken); if (result != null && result.Items.Any()) { items.AddRange(result.Items); while (!string.IsNullOrEmpty(result.NextLink)) { result = await GraphHelper.GetAsync <RestResultCollection <Microsoft365Group> >(httpClient, result.NextLink, accessToken); if (result != null && result.Items.Any()) { items.AddRange(result.Items); } } } if (includeSiteUrl || includeOwners) { var chunks = BatchUtility.Chunk(items.Select(g => g.Id.ToString()), 20); if (includeOwners) { foreach (var chunk in chunks) { var ownerResults = await BatchUtility.GetObjectCollectionBatchedAsync <Microsoft365User>(httpClient, accessToken, chunk.ToArray(), "/groups/{0}/owners"); foreach (var ownerResult in ownerResults) { items.First(i => i.Id.ToString() == ownerResult.Key).Owners = ownerResult.Value; } } } if (includeSiteUrl) { foreach (var chunk in chunks) { var results = await BatchUtility.GetPropertyBatchedAsync(httpClient, accessToken, chunk.ToArray(), "/groups/{0}/sites/root", "webUrl"); //var results = await GetSiteUrlBatchedAsync(httpClient, accessToken, chunk.ToArray()); foreach (var batchResult in results) { items.First(i => i.Id.ToString() == batchResult.Key).SiteUrl = batchResult.Value; } } } } return(items); }
public static async Task AddUsersAsync(HttpClient httpClient, string accessToken, string groupId, string[] upn, string role) { var chunks = BatchUtility.Chunk(upn, 20); foreach (var chunk in chunks) { var results = await BatchUtility.GetPropertyBatchedAsync(httpClient, accessToken, chunk.ToArray(), "/users/{0}", "id"); var teamChannelMember = new List <TeamChannelMember>(); foreach (var userid in results.Select(r => r.Value)) { teamChannelMember.Add(new TeamChannelMember() { Roles = new List <string> { role }, UserIdentifier = $"https://{PnPConnection.Current.GraphEndPoint}/v1.0/users('{userid}')" }); } await GraphHelper.PostAsync(httpClient, $"v1.0/teams/{groupId}/members/add", new { values = teamChannelMember }, accessToken); } }