Beispiel #1
0
        private bool IsMetadataMissing(ImportData importData, out MissingMetadata result)
        {
            MetadataService metadataService = GetMetadataService();

            result = new MissingMetadata();

            foreach (IGrouping <string, DynamicEntity> group in importData.Entities.GroupBy(e => e.Name))
            {
                string entityName = group.Key;

                EntityMetadata entityMetadata = MetadataUtility.RetrieveEntityMetadata(metadataService, entityName);
                if (entityMetadata == null)
                {
                    result.MissingEntities.Add(entityName);
                }
                else
                {
                    IEnumerable <String> distinctAttributes = group
                                                              .SelectMany(e => e.Properties.Select(prop => prop.Name))
                                                              .Distinct();

                    List <string> missingAttributes =
                        distinctAttributes
                        .Except(entityMetadata.Attributes.Select(am => am.LogicalName))
                        .ToList();

                    if (missingAttributes.Count > 0)
                    {
                        result.AddMissingAttributes(entityName, missingAttributes);
                    }
                }
            }

            return(result.MissingEntities.Any() || result.MissingAttributes.Any());
        }
Beispiel #2
0
        private void ImportRecord(DynamicEntity entity)
        {
            string statecode  = "";
            int    statuscode = -1;

            if (entity.Properties.Contains("statecode"))
            {
                statecode = entity["statecode"].ToString();
                entity.Properties.Remove("statecode");
            }

            if (entity.Properties.Contains("statuscode"))
            {
                statuscode = ((Status)entity["statuscode"]).Value;
                entity.Properties.Remove("statuscode");
            }

            string primaryAttribute      = MetadataUtility.RetrievePrimaryAttribute(_metadataService, entity.Name);
            string primaryAttributeValue = entity.Properties.Contains(primaryAttribute) ? entity[primaryAttribute].ToString() : "";
            Guid   id                  = entity.Properties.OfType <KeyProperty>().First().Value.Value;
            Guid   potentialNewId      = Guid.Empty;
            int    matchingRecordCount = GetMatchingRecordCount(entity.Name, id, primaryAttributeValue, ref potentialNewId);

            switch (matchingRecordCount)
            {
            case 0:
                _crmService.Create(entity);
                ReportDetail(String.Format("Created {0} with id of {1}.", entity.Name, id));
                break;

            case 1:
                if (potentialNewId != Guid.Empty)
                {
                    id = potentialNewId;
                    entity.Properties.OfType <KeyProperty>().First().Value.Value = potentialNewId;
                }
                _crmService.Update(entity);
                ReportDetail(String.Format("Updated {0} with id of {1}.", entity.Name, id));
                break;

            default:
                ReportError(String.Format("Cannot import {0} with id of {1} because it matches more than one record in the target system based on attribute {2}.", entity.Name, id, primaryAttribute));
                break;
            }

            if (matchingRecordCount < 2 && !String.IsNullOrEmpty(statecode))
            {
                DynamicEntityUtility.SetStateDynamicEntity(_crmService, entity.Name, id, statecode, statuscode);
                ReportDetail(String.Format("Set state on {0} with id of {1}.", entity.Name, id));
            }

            _currentRecordCount++;
            _entityRecordCounts[entity.Name]++;

            int percent = (int)Math.Round(100.0 * _currentRecordCount / _totalRecordCount);

            percent = percent > 100 ? 100 : percent;
            OnImportProgressChanged(new ProgressChangedEventArgs(percent, null));
        }
Beispiel #3
0
        private ColumnSet GetColumnsForExport(string entityName)
        {
            EntityMetadata entityMetadata = MetadataUtility.RetrieveEntityMetadata(_metadataService, entityName);

            var findValidColumnsQuery = from attribute in entityMetadata.Attributes
                                        where (attribute.ValidForCreate.Value && attribute.ValidForUpdate.Value && String.IsNullOrEmpty(attribute.AttributeOf)) ||
                                        attribute.LogicalName == "statecode"
                                        select attribute.LogicalName;

            return(new ColumnSet(findValidColumnsQuery.ToArray()));
        }
        public static IEnumerable <DynamicEntity> GetByIdOrPrimaryAttribute(CrmService _crmService, MetadataService metadataService, string entityName, Guid id, string primaryAttributeValue, params string[] fields)
        {
            List <DynamicEntity> resultList = new List <DynamicEntity>();
            DynamicEntity        ret        = GetById(_crmService, entityName, id);

            if (ret == null)
            {
                string          primaryAttribute = MetadataUtility.RetrievePrimaryAttribute(metadataService, entityName);
                QueryExpression query            = new QueryExpression(entityName)
                {
                    ColumnSet = new ColumnSet(fields)
                };

                ConditionExpression primaryAttributeExpression = new ConditionExpression(primaryAttribute.ToLower(), ConditionOperator.Equal, primaryAttributeValue);
                query.Criteria.Conditions.Add(primaryAttributeExpression);

                RetrieveMultipleRequest request = new RetrieveMultipleRequest()
                {
                    Query = query,
                    ReturnDynamicEntities = true
                };

                RetrieveMultipleResponse response = (RetrieveMultipleResponse)_crmService.Execute(request);

                if (response.BusinessEntityCollection.BusinessEntities.Count > 0)
                {
                    resultList.AddRange(response.BusinessEntityCollection.BusinessEntities.ConvertAll <DynamicEntity>(x => (DynamicEntity)x));
                }
            }
            else
            {
                resultList.Add(ret);
            }

            return(resultList);
        }