public CRMEntity GetConfigurationEntity(IOrganizationService service, string configurationName)
        {
            QueryExpression qe = new QueryExpression("msdyusd_configuration");

            qe.ColumnSet = new ColumnSet(true);
            qe.Criteria.AddCondition("msdyusd_name", ConditionOperator.Equal, configurationName);


            EntityCollection results = service.RetrieveMultiple(qe);

            if (results == null || results.Entities == null || results.Entities.Count != 1)
            {
                return(null);
            }

            Entity    configuration          = results.Entities[0];
            CRMRecord crmConfigurationRecord = configuration.ToCRMRecord();

            CRMEntity crmEntity = new CRMEntity
            {
                LogicalName = "msdyusd_configuration",
                CRMRecords  = new List <CRMRecord> {
                    crmConfigurationRecord
                },
                RecordCount = 1
            };

            return(crmEntity);
        }
Ejemplo n.º 2
0
        public CRMEntity GetCRMEntity(IOrganizationService service, QueryExpression query)
        {
            EntityCollection results = service.RetrieveMultiple(query);

            if (results != null && results.Entities != null && results.Entities.Count > 0)
            {
                List <string> entityAttributes = GetEntityAttributes(service, results.Entities[0].LogicalName);

                List <CRMRecord> records = new List <CRMRecord>();
                foreach (Entity entity in results.Entities)
                {
                    //This is needed as the fetch does not return null values in the results
                    //This is useful in making sure that null values in source record are sent to the target record for a attribute
                    foreach (string attribute in entityAttributes)
                    {
                        if (!entity.Contains(attribute))
                        {
                            entity[attribute] = null;
                        }
                    }

                    CRMRecord crmRecord = entity.ToCRMRecord();
                    records.Add(crmRecord);
                }


                CRMEntity crmEntity = new CRMEntity {
                    CRMRecords = records, LogicalName = results.EntityName, RecordCount = results.Entities.Count
                };
                return(crmEntity);
            }


            return(null);
        }
        public static CRMRecord ToCRMRecord(this Entity entity)
        {
            CRMRecord crmrecord = null;

            if (entity == null)
            {
                return(crmrecord);
            }

            List <string> attributesToExclude = Util.GetCRMAttributesToExclude();

            List <CRMAttribute> crmAttributes = new List <CRMAttribute>();

            foreach (var attribute in entity.Attributes)
            {
                if (attributesToExclude.Contains(attribute.Key))
                {
                    continue;
                }

                CRMAttribute crmAttribute = GetCRMAttribute(attribute);
                crmAttributes.Add(crmAttribute);
            }

            crmrecord = new CRMRecord {
                LogicalName = entity.LogicalName, Id = entity.Id, CRMAttributes = crmAttributes
            };

            return(crmrecord);
        }
Ejemplo n.º 4
0
        public static Entity ToEntity(this CRMRecord crmRecord, Dictionary <Guid, Guid> sourceTargetIdMappings)
        {
            Entity entity = null;

            if (crmRecord == null)
            {
                return(entity);
            }

            AttributeCollection attributes = new AttributeCollection();

            foreach (var crmAttribute in crmRecord.CRMAttributes)
            {
                if (CrmAttributeToExclude.Contains(crmAttribute.LogicalName) || (crmAttribute.AttributeType != null && crmAttribute.AttributeType.ToLower() == "guid"))
                {
                    continue;
                }

                attributes[crmAttribute.LogicalName] = GetEntityAttribute(crmAttribute, sourceTargetIdMappings);
            }
            entity = new Entity(crmRecord.LogicalName);

            if (crmRecord.Id != Guid.Empty)
            {
                entity.Id = crmRecord.Id;
            }
            entity.Attributes = attributes;
            return(entity);
        }
Ejemplo n.º 5
0
        protected ImportResult GetImportResult(BulkResponse bulkResponse, CRMEntity crmEntity)
        {
            ImportResult  importResult  = new ImportResult();
            List <String> errorMessages = new List <string>();


            if (crmEntity == null)
            {
                return(importResult);
            }

            List <CRMRecord> crmRecords = crmEntity.CRMRecords;

            foreach (BulkResponseItem bulkResponseItem in bulkResponse.Responses)
            {
                string requestId = bulkResponseItem.BulkRequestId;
                if (!bulkResponseItem.Success)
                {
                    string errorMessage = null;
                    if (crmEntity.IsIntersect)
                    {
                        errorMessage = crmEntity.LogicalName + "," + requestId + "," + bulkResponseItem.Action + "," + bulkResponseItem.Error;
                    }
                    else
                    {
                        if (string.IsNullOrWhiteSpace(requestId))
                        {
                            continue;
                        }

                        Guid      id = Guid.Parse(requestId);
                        CRMRecord failedCrmRecord = crmRecords.FirstOrDefault(x => x.Id == id);


                        string failedId = (failedCrmRecord.Id != Guid.Empty) ? failedCrmRecord.Id.ToString() : "";
                        errorMessage = crmEntity.LogicalName + "," + failedId + "," + bulkResponseItem.Action + "," + bulkResponseItem.Error;
                    }

                    errorMessages.Add(errorMessage);
                }
            }



            importResult.EntityLogicalName = crmEntity.LogicalName;
            importResult.Errors            = errorMessages;
            importResult.FailureCount      = errorMessages.Count;
            importResult.TotalProcessed    = bulkResponse.Responses.Count;
            importResult.SuccessCount      = importResult.TotalProcessed - importResult.FailureCount;
            importResult.CreateCount       = bulkResponse.Responses.Where(x => x.Action != null && x.Action.ToLower() == "create" && x.Success).Count();
            importResult.UpdateCount       = bulkResponse.Responses.Where(x => x.Action != null && x.Action.ToLower() == "update" && x.Success).Count();
            importResult.AssociateCount    = bulkResponse.Responses.Where(x => x.Action != null && x.Action.ToLower() == "associaterequest" && x.Success).Count();
            importResult.DisassociateCount = bulkResponse.Responses.Where(x => x.Action != null && x.Action.ToLower() == "disassociaterequest" && x.Success).Count();
            return(importResult);
        }
Ejemplo n.º 6
0
        public static EntityReference ToEntityReference(this CRMRecord crmRecord)
        {
            string primaryAttributeName = crmRecord.LogicalName.StartsWith("uii") ? "uii_name" : "msdyusd_name";


            CRMAttribute primaryNameAtt        = (crmRecord.CRMAttributes != null) ? crmRecord.CRMAttributes.Where(x => x.LogicalName == primaryAttributeName).FirstOrDefault() : null;
            string       primaryAttributeValue = (primaryNameAtt != null && primaryNameAtt.Value != null) ? primaryNameAtt.Value.ToString() : null;

            EntityReference entityReference = new EntityReference
            {
                Id          = crmRecord.Id,
                LogicalName = crmRecord.LogicalName,
                Name        = primaryAttributeValue
            };

            return(entityReference);
        }
Ejemplo n.º 7
0
        public List <IItem> GetListItems(ISiteSetting siteSetting, List <CamlOrderBy> orderBys, CamlFilters filters, List <CamlFieldRef> viewFields, CamlQueryOptions queryOptions, string webUrl, string listName, out string listItemCollectionPositionNext, out int itemCount)
        {
            try
            {
                List <IItem>         results             = new List <IItem>();
                IOrganizationService organizationService = GetClientContext(siteSetting);

                var query = new QueryExpression(listName);
                query.ColumnSet.AddColumns(viewFields.Select(t => t.Name).ToArray());
                query.Criteria = GetFilterExpression(filters);
                //query.Criteria.Filters[0].FilterOperator = LogicalOperator.
                EntityCollection entities = organizationService.RetrieveMultiple(query);
                itemCount = entities.TotalRecordCount;
                listItemCollectionPositionNext = string.Empty;

                foreach (Entity entity in entities.Entities)
                {
                    CRMRecord record = new CRMRecord(siteSetting.ID);
                    foreach (CamlFieldRef viewField in viewFields)
                    {
                        string viewFieldName = viewField.Name;
                        if (entity.Attributes.ContainsKey(viewFieldName) == true)
                        {
                            string value    = string.Empty;
                            object objValue = entity.Attributes[viewFieldName];
                            if (objValue != null)
                            {
                                if (objValue is EntityReference)
                                {
                                    EntityReference entityReference = (EntityReference)objValue;
                                    value = entityReference.Id.ToString() + ";#" + entityReference.Name;
                                }
                                else if (objValue is EntityReferenceCollection)
                                {
                                    EntityReferenceCollection entityReferences = (EntityReferenceCollection)objValue;
                                    for (int i = 0; i < entityReferences.Count; i++)
                                    {
                                        EntityReference entityReference = entityReferences[i];
                                        if (i > 0)
                                        {
                                            value += ";#";
                                        }
                                        value += entityReference.Id.ToString() + ";#" + entityReference.Name;
                                    }
                                }
                                else if (objValue is DateTime)
                                {
                                    value = ((DateTime)objValue).ToString("yyyy-MM-ddTHH:mm:ssZ");
                                }
                                else
                                {
                                    value = entity.Attributes[viewFieldName].ToString();
                                }
                            }
                            record.Properties.Add(viewFieldName, value);
                        }
                    }

                    results.Add(record);
                }

                return(results);
            }
            catch (Exception ex)
            {
                string errorMessage = errorMessage = ex.Message;

                errorMessage += Environment.NewLine + ex.StackTrace;
                Logger.Info(errorMessage, "Service");


                string methodName = System.Reflection.MethodBase.GetCurrentMethod().Name;
                //LogManager.LogAndShowException(methodName, ex);
                throw ex;
            }
        }