Exemple #1
0
        public void DeserializeData(List <Entity> entities, bool createOnly)
        {
            foreach (var e in entities)
            {
                var metadata = ReferenceResolution.GetMetadata(Service, e.LogicalName);

                string lookupField = null;
                if (lookupField == null)
                {
                    lookupField = metadata.PrimaryIdAttribute;
                }

                ReferenceResolution.ResolveReferences(Service, e);

                if (metadata.IsIntersect == null || !metadata.IsIntersect.Value)
                {
                    if (lookupField != metadata.PrimaryIdAttribute && !e.Contains(lookupField))
                    {
                        throw new InvalidPluginExecutionException("Lookup error: The entity being imported does not have '" + lookupField + "' attribute");
                    }
                    QueryExpression qe = new QueryExpression(e.LogicalName);
                    qe.Criteria.AddCondition(new ConditionExpression(lookupField, ConditionOperator.Equal,
                                                                     lookupField == metadata.PrimaryIdAttribute ? e.Id : e[lookupField]));

                    var existing = Service.RetrieveMultiple(qe).Entities.FirstOrDefault();
                    if (existing != null)
                    {
                        if (!createOnly)
                        {
                            e.Id = existing.Id;
                            Service.Update(e);
                        }
                    }
                    else
                    {
                        Service.Create(e);
                    }
                }
                else
                {
                    if (e.LogicalName == "listmember")
                    {
                        if (e.Contains("entityid") && e.Contains("listid"))
                        {
                            QueryExpression qe = new QueryExpression("listmember");
                            qe.Criteria.AddCondition(new ConditionExpression("entityid", ConditionOperator.Equal, ((EntityReference)e["entityid"]).Id));
                            qe.Criteria.AddCondition(new ConditionExpression("listid", ConditionOperator.Equal, ((EntityReference)e["listid"]).Id));
                            bool exists = Service.RetrieveMultiple(qe).Entities.FirstOrDefault() != null;
                            if (!exists)
                            {
                                AddMemberListRequest amlr = new AddMemberListRequest();
                                amlr.EntityId = ((EntityReference)e["entityid"]).Id;
                                amlr.ListId   = ((EntityReference)e["listid"]).Id;
                                Service.Execute(amlr);
                            }
                        }
                    }
                    else
                    {
                        foreach (var r in metadata.ManyToManyRelationships)
                        {
                            if (r.IntersectEntityName == e.LogicalName)
                            {
                                if (e.Contains(r.Entity1IntersectAttribute) &&
                                    e.Contains(r.Entity2IntersectAttribute)
                                    )
                                {
                                    QueryExpression qe = new QueryExpression(r.IntersectEntityName);
                                    qe.Criteria.AddCondition(new ConditionExpression(r.Entity1IntersectAttribute, ConditionOperator.Equal, (Guid)e[r.Entity1IntersectAttribute]));
                                    qe.Criteria.AddCondition(new ConditionExpression(r.Entity2IntersectAttribute, ConditionOperator.Equal, (Guid)e[r.Entity2IntersectAttribute]));
                                    bool exists = Service.RetrieveMultiple(qe).Entities.FirstOrDefault() != null;
                                    if (!exists &&
                                        Common.RecordExists(Service, r.Entity1LogicalName, r.Entity1IntersectAttribute, (Guid)e[r.Entity1IntersectAttribute]) &&
                                        Common.RecordExists(Service, r.Entity2LogicalName, r.Entity2IntersectAttribute, (Guid)e[r.Entity2IntersectAttribute])
                                        )
                                    {
                                        Relationship rs = new Relationship(r.SchemaName);
                                        EntityReferenceCollection collection = new EntityReferenceCollection();

                                        collection.Add(new EntityReference(r.Entity2IntersectAttribute)
                                        {
                                            Id = (Guid)e[r.Entity2IntersectAttribute]
                                        });

                                        Service.Associate(r.Entity1LogicalName,
                                                          (Guid)e[r.Entity1IntersectAttribute],
                                                          rs,
                                                          collection);
                                    }
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
        //No need for buid backshift - the point is to have new guids
        public KeyValuePair <string, object> ConvertToAttribute(IOrganizationService service, string entityName, int guidShift)
        {
            if (Value == null)
            {
                return(new KeyValuePair <string, object>(Key, null));
            }

            Object attributeValue = null;

            TypeName = ReferenceResolution.GetAttributeCodeTypeName(service, entityName, Key).ToLower();
            if (TypeName == null)
            {
                throw new Exception("Cannot find type name for " + entityName + ":" + Key);
            }
            if (TypeName.Contains("guid"))
            {
                attributeValue = Guid.Parse(Value.ToString());
            }
            else if (TypeName.Contains("entityreference"))
            {
                string[] pair = ((string)Value).Split(':');
                attributeValue = new EntityReference(pair[1], Guid.Parse(pair[0]));
            }
            else if (TypeName.Contains("string"))
            {
                attributeValue = (string)Value;
            }
            else if (TypeName.Contains("int"))
            {
                attributeValue = int.Parse((string)Value);
            }
            else if (TypeName.Contains("double"))
            {
                attributeValue = double.Parse((string)Value);
            }
            else if (TypeName.Contains("float"))
            {
                attributeValue = float.Parse((string)Value);
            }
            else if (TypeName.Contains("decimal"))
            {
                attributeValue = decimal.Parse((string)Value);
            }
            else if (TypeName.Contains("datetime"))
            {
                attributeValue = DateTime.Parse((string)Value);
            }
            else if (TypeName.Contains("optionsetvalue"))
            {
                attributeValue = new OptionSetValue(int.Parse((string)Value));
            }
            else if (TypeName.Contains("money"))
            {
                attributeValue = new Money(decimal.Parse((string)Value));
            }
            else if (TypeName.Contains("boolean"))
            {
                attributeValue = bool.Parse((string)Value);
            }


            KeyValuePair <string, object> a = new KeyValuePair <string, object>(Key, attributeValue);

            return(a);
        }