public async Task <IEnumerable <PublishedAssignment> > AddRangeAsync(PublishedProject publishedProject, IEnumerable <AssignmentModel> assignmentModels) { if (publishedProject == null) { throw new ArgumentNullException(nameof(publishedProject)); } if (assignmentModels == null) { throw new ArgumentNullException(nameof(assignmentModels)); } List <PublishedAssignment> result = new List <PublishedAssignment>(); try { if ((await RequirementsAreMet(publishedProject, assignmentModels))) { // Ensure I have it check it out await _projectClient.CheckInAsync(publishedProject); var draftProject = await _projectClient.CheckOutAsync(publishedProject); foreach (var assignmentModel in assignmentModels) { var draftAssignment = draftProject.Assignments.Add(_mapper.Map <AssignmentCreationInformation>(assignmentModel)); draftAssignment = _mapper.Map(assignmentModel, draftAssignment); //var queuedJob = draftProject.Update(); //WaitForQueuedJobToComplete(queuedJob, $"updating a project with name {publishedProject.Name}"); //_projectContext.ExecuteQuery(); } await _projectClient.PublishAndUpdate(draftProject, publishedProject.Name); await _projectClient.CheckInAsync(publishedProject); // reload the project after re-publishing it var _republishedProjects = _projectContext.LoadQuery( _projectContext.Projects .Where(p => p.Id == publishedProject.Id).IncludeWithDefaultProperties(p => p.Assignments)); await _projectContext.ExecuteQueryAsync(); result = _republishedProjects.First().Assignments .Where(a => assignmentModels.Any(am => am.Id == a.Id)).ToList(); } } catch (Exception ex) { // TODO: LOG ERROR! throw new CsomClientException($"Unexcepted error adding a new assignments. " + $"Published project name is {publishedProject.Name}. " + $"Project context url is {_projectContext.Url}.", ex); } return(result); }
public async Task <IEnumerable <PublishedTask> > AddRangeAsync(PublishedProject publishedProject, IEnumerable <TaskModel> taskModels) { if (publishedProject == null) { throw new ArgumentNullException(nameof(publishedProject)); } if (taskModels == null) { throw new ArgumentNullException(nameof(taskModels)); } IList <PublishedTask> publishedTasks = new List <PublishedTask>(); IList <Guid> publishedTaskIds = new List <Guid>(); try { // Ensure I have it check it out await _projectClient.CheckInAsync(publishedProject); var draftProject = await _projectClient.CheckOutAsync(publishedProject); _projectContext.Load(draftProject.Tasks); await _projectContext.ExecuteQueryAsync(); var existingProjectTaskIds = draftProject.Tasks.Select(t => t.Id).ToList(); foreach (var taskModel in taskModels) { // Ensure no other task with same id exist if (!existingProjectTaskIds.Any(id => id == taskModel.Id)) { var draftTask = draftProject.Tasks.Add(_mapper.Map <TaskCreationInformation>(taskModel)); draftTask = _mapper.Map(taskModel, draftTask); SetCustomFields(draftTask, taskModel.CustomFields); publishedTaskIds.Add(taskModel.Id); } } if (publishedTaskIds.Any()) { await _projectClient.PublishAndUpdate(draftProject, publishedProject.Name); foreach (var id in publishedTaskIds) { publishedTasks.Add(await GetByIdAsync(publishedProject, id, includeCustomFields: true)); } } await _projectClient.CheckInAsync(publishedProject); } catch (Exception ex) { // TODO: LOG ERROR! throw new CsomClientException($"Unexcepted error adding a tasks. " + $"Published project name is {publishedProject.Name}. " + $"Project context url is {_projectContext.Url}.", ex); } return(publishedTasks); }