private List <crm_Employee> GetCrmEmployees(OrganizationServiceProxy _service, string entityName, string[] columns) { List <Entity> results = GetEntities(_service, entityName, columns); List <crm_Employee> employees = new List <crm_Employee>(); try { foreach (var item in results) { crm_Employee employee = new crm_Employee() { EmployeeId = item.Attributes.Contains("ihr_employeeid") ? (Guid)item.Attributes["ihr_employeeid"] : Guid.Empty, FirstName = item.Attributes.Contains("ihr_firstname") ? (string)item.Attributes["ihr_firstname"] : string.Empty, LastName = item.Attributes.Contains("ihr_lastname") ? (string)item.Attributes["ihr_lastname"] : string.Empty, EndDate = item.Attributes.Contains("ihr_enddate") ? (DateTime?)item.Attributes["ihr_enddate"] : null, ManagerId = item.Attributes.Contains("ihr_managerid") ? ((EntityReference)item.Attributes["ihr_managerid"]).Id : Guid.Empty, OfficeId = item.Attributes.Contains("ihr_office") ? ((OptionSetValue)item.Attributes["ihr_office"]).Value : -1 }; employees.Add(employee); } } catch (Exception ex) { throw new NotSupportedException(ex.Message); } return(employees); }
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; } }
private Employee CreateEmployee(crm_ProjectParticipation crm_projectParticipation, crm_Employee employee, crm_Employee manager, List <OptionalValue> offices) { return(new Employee() { EmployeeId = employee.EmployeeId, FirstName = employee.FirstName, LastName = employee.LastName, Manager = manager != null?string.Format("{0} {1}", manager.FirstName, manager.LastName) : string.Empty, Office = employee.OfficeId != -1 ? offices.FirstOrDefault(p => p.Value == employee.OfficeId).Text : "<no assignment>", Login = string.Format("{0}{1}", employee.FirstName.ToLower().Remove(1), employee.LastName.IndexOf('-') != -1 ? employee.LastName.Remove(employee.LastName.IndexOf('-')).ToLower() : employee.LastName.ToLower()), StartDate = crm_projectParticipation.DateFrom.HasValue ? crm_projectParticipation.DateFrom.Value.ToShortDateString() : string.Empty, EndDate = crm_projectParticipation.DateTo.HasValue ? crm_projectParticipation.DateTo.Value.ToShortDateString() : string.Empty }); }