private List <crm_Project> GetCrmProjects(OrganizationServiceProxy _service, string entityName, string[] columns) { List <Entity> results = GetEntities(_service, entityName, columns); List <crm_Project> projects = new List <crm_Project>(); try { foreach (var item in results) { crm_Project project = new crm_Project() { ProjectId = item.Attributes.Contains("ihr_projectid") ? (Guid)item.Attributes["ihr_projectid"] : Guid.Empty, ProjectCode = item.Attributes.Contains("ihr_projectcode") ? (string)item.Attributes["ihr_projectcode"] : string.Empty, ProjectName = item.Attributes.Contains("ihr_name") ? (string)item.Attributes["ihr_name"] : string.Empty, ClientName = item.Attributes.Contains("ihr_client") ? (string)item.Attributes["ihr_client"] : string.Empty, ProjectStageId = item.Attributes.Contains("statecode") ? ((OptionSetValue)item.Attributes["statecode"]).Value : -1 }; projects.Add(project); } } catch (Exception ex) { throw new NotSupportedException(ex.Message); } return(projects); }
public List <ProjectParticipation> GetProjectParticipation() { try { _logger.Info("Executing CRM Query"); if (_serverConfig == null) { InitializeConfiguration(); } using (var _service = ServerConnection.GetOrganizationProxy(_serverConfig)) { var query = new QueryExpression(); Entity projectParticipationsEntity = new Entity("ihr_projectparticipation"); query.EntityName = projectParticipationsEntity.LogicalName; query.ColumnSet = new ColumnSet("ihr_projectid", "ihr_employeeid", "ihr_projectparticipationid", "ihr_datefrom", "ihr_dateto"); EntityCollection retrieved = _service.RetrieveMultiple(query); List <Entity> entities = retrieved.Entities.ToList(); _logger.Info("CRM Query returned: " + entities.Count + " project participations"); var projectParticipations = new List <ProjectParticipation>(); List <crm_Project> projects = GetCrmProjects(_service, "ihr_project", new string[] { "ihr_projectid", "ihr_projectcode", "ihr_name", "ihr_client", "statecode" }); List <crm_Employee> employees = GetCrmEmployees(_service, "ihr_employee", new string[] { "ihr_employeeid", "ihr_firstname", "ihr_lastname", "ihr_enddate", "ihr_managerid", "ihr_office" }); var offices = GetOptionSet(ihr_employee.EntityLogicalName, "ihr_Office", _service); var projStages = GetOptionSet(ihr_project.EntityLogicalName, "statecode", _service); foreach (var item in entities) { crm_ProjectParticipation crm_projectParticipation = CreateProjectParticipation(item); crm_Project project = projects.FirstOrDefault(p => p.ProjectId == crm_projectParticipation.ProjectId); crm_Employee employee = employees.FirstOrDefault(p => p.EmployeeId == crm_projectParticipation.EmployeeId); crm_Employee manager = employee.ManagerId != Guid.Empty ? employees.FirstOrDefault(p => p.EmployeeId == employee.ManagerId) : null; var projectParticipation = projectParticipations.FirstOrDefault(p => p.ProjectId == project.ProjectId); if (projectParticipation != null && (!employee.EndDate.HasValue || employee.EndDate >= DateTime.Now)) { projectParticipation.Allocations.Add(CreateEmployee(crm_projectParticipation, employee, manager, offices)); } else { IList <Employee> emplList = new List <Employee>(); if ((!employee.EndDate.HasValue || employee.EndDate >= DateTime.Now)) { emplList.Add(CreateEmployee(crm_projectParticipation, employee, manager, offices)); } projectParticipations.Add(new ProjectParticipation() { Name = project.ProjectName, Code = project.ProjectCode, ProjectParticipationId = crm_projectParticipation.ProjectParticipationId, ProjectStage = project.ProjectStageId != -1 ? projStages.FirstOrDefault(p => p.Value == project.ProjectStageId).Text : string.Empty, Allocations = emplList, ProjectId = project.ProjectId }); } } _logger.Info("CRM Query finished"); return(projectParticipations); } } catch (Exception e) { _logger.FatalException("CRM Query failed", e); throw; } }