Utility class to hold various operations that are common when working with Dynamics CRM.
        /// <summary>
        /// Converts all representations of the state property to an integer.
        /// </summary>
        /// <param name="stateName">The state to get the integer value of as a <c>string</c>.</param>
        /// <param name="entityName">The name of the entity type to get the state code for.</param>
        /// <param name="adapter">The <see cref="DynamicCrmAdapter"/> to use for calling into a CRM for resolving that state.</param>
        /// <returns>An <c>int</c> the represents the state.</returns>
        public static int ConvertStateNameToValue(string stateName, string entityName, DynamicCrmAdapter adapter)
        {
            if (adapter == null)
            {
                throw new ArgumentNullException("adapter");
            }

            RetrieveAttributeRequest attribReq = new RetrieveAttributeRequest()
            {
                EntityLogicalName = entityName, LogicalName = "statecode", RetrieveAsIfPublished = true
            };

            // Get the attribute metadata for the state attribute.
            RetrieveAttributeResponse metadataResponse = (RetrieveAttributeResponse)adapter.OrganizationService.Execute(attribReq);
            StateAttributeMetadata    picklistAttrib   = (StateAttributeMetadata)metadataResponse.AttributeMetadata;

            var picklistValue = from option in picklistAttrib.OptionSet.Options
                                where option.Label.UserLocalizedLabel.Label.ToUpperInvariant() == stateName.ToUpperInvariant()
                                select option.Value;

            // Ensure that both the returned list and the first item in the returned list are not null or empty.
            if ((picklistValue.Count() > 0) && (picklistValue.First() != null))
            {
                return(picklistValue.First().Value);
            }

            return(CRM2011AdapterUtilities.GetDefaultStateCodeValue(stateName));
        }
        private System.Collections.ObjectModel.Collection <ObjectProvider> GetObjectProviders()
        {
            this.providers = new System.Collections.ObjectModel.Collection <ObjectProvider>();
            ObjectDefinition objDef = null;

            // This will need to be modified to reflect utilizing a different directory
            foreach (string configFileName in Directory.GetFiles(this.GetConfigPath <DynamicCrmAdapterDestination>()))
            {
                using (var fs = File.OpenRead(configFileName))
                {
                    using (var xr = XmlReader.Create(fs))
                    {
                        var serializer = new XmlSerializer(typeof(ObjectDefinition));
                        objDef = (ObjectDefinition)serializer.Deserialize(xr);
                    }
                }

                if (!IsStaticObjectProvider(objDef))
                {
                    DynamicObjectProvider dynObject = new DynamicObjectProvider()
                    {
                        Adapter = this, Id = GetDynamicProviderId(objDef), DisplayName = objDef.RootDefinition.DisplayName, Name = CRM2011AdapterUtilities.GetObjectProviderName(objDef)
                    };
                    this.providers.Add(dynObject);
                }

                if (objDef.RootDefinition.Name.Equals("OptionList"))
                {
                    PicklistObjectProvider pickObject = new PicklistObjectProvider()
                    {
                        Adapter = this
                    };
                    this.providers.Add(pickObject);
                }
            }

            return(this.providers);
        }
        /// <summary>
        /// Gets an instance of the <c>customeraddress</c> class.
        /// </summary>
        /// <param name="addressIntegrationKeyValue">The value of the address's dynamics_integrationkey property to query for.</param>
        /// <param name="parentKey">The parent of this address to use when querying for the existence of this address instance.</param>
        /// <param name="adapter">An instance of the <c>CRM2011Adapter</c> class to use when calling CRM.</param>
        /// <param name="addressIntegrationKeyProperty">The key attribute on the <c>customeraddress</c> that the supplied value is for.</param>
        /// <returns>A new instance with it's dynamics_integrationkey initialized to the value supplied or an existing instance.</returns>
        public static Entity GetDynamicAddressInstance(string addressIntegrationKeyValue, Guid parentKey, DynamicCrmAdapter adapter, string addressIntegrationKeyProperty)
        {
            if (parentKey == null || adapter == null)
            {
                throw new AdapterException(string.Format(CultureInfo.CurrentCulture, Resources.ArgumentNullExceptionMessage))
                      {
                          ExceptionId = AdapterException.SystemExceptionGuid
                      };
            }

            if (string.IsNullOrEmpty(addressIntegrationKeyProperty))
            {
                throw new AdapterException(string.Format(CultureInfo.CurrentCulture, Resources.AddressIntegrationKeyPropertyInvalidMessage))
                      {
                          ExceptionId = ErrorCodes.AddressIntegrationKeyPropertyException
                      };
            }

            RetrieveMultipleRequest retrieveRequest = new RetrieveMultipleRequest();

            KeyValuePair <string, string>[] propertyValues = new KeyValuePair <string, string> [2];
            propertyValues[0]     = new KeyValuePair <string, string>(addressIntegrationKeyProperty, addressIntegrationKeyValue);
            propertyValues[1]     = new KeyValuePair <string, string>("parentid", parentKey.ToString());
            retrieveRequest.Query = CRM2011AdapterUtilities.GetMultipartQueryExpression(LogicalOperator.And, "customeraddress", propertyValues);
            RetrieveMultipleResponse retrieveResponse = (RetrieveMultipleResponse)adapter.OrganizationService.Execute(retrieveRequest);
            Entity returnedEntity = null;

            if (retrieveResponse.EntityCollection.Entities.Count == 1)
            {
                returnedEntity = retrieveResponse.EntityCollection.Entities[0] as Entity;
                returnedEntity[CRM2011AdapterUtilities.IsNew] = false;
                return(returnedEntity);
            }
            else if (retrieveResponse.EntityCollection.Entities.Count < 1)
            {
                // this is a new entity instance and we need to map the provided data onto the DynamicsEntity
                returnedEntity = new Entity()
                {
                    LogicalName = "customeraddress"
                };
                if (addressIntegrationKeyProperty.Equals("customeraddressid", StringComparison.OrdinalIgnoreCase))
                {
                    returnedEntity[addressIntegrationKeyProperty] = new Guid(addressIntegrationKeyValue);
                }
                else
                {
                    returnedEntity[addressIntegrationKeyProperty] = addressIntegrationKeyValue;
                }

                returnedEntity[CRM2011AdapterUtilities.IsNew] = true;
                return(returnedEntity);
            }
            else
            {
                throw new AdapterException(
                          string.Format(
                              CultureInfo.CurrentCulture,
                              Resources.MultipleDynamicEntitiesReturnedExceptionMessage,
                              "customeraddress",
                              addressIntegrationKeyProperty,
                              addressIntegrationKeyValue))
                      {
                          ExceptionId = ErrorCodes.MultipleCustomerAddressResult
                      };
            }
        }
예제 #4
0
        private System.Collections.ObjectModel.Collection <ObjectProvider> GetObjectProviders()
        {
            this.providers = new System.Collections.ObjectModel.Collection <ObjectProvider>();
            ObjectDefinition objDef = null;

            foreach (string configFileName in Directory.GetFiles(this.GetConfigPath <DynamicCrmAdapter>()))
            {
                using (var fs = File.OpenRead(configFileName))
                {
                    using (var xr = XmlReader.Create(fs))
                    {
                        var serializer = new XmlSerializer(typeof(ObjectDefinition));
                        objDef = (ObjectDefinition)serializer.Deserialize(xr);
                    }
                }

                if (!IsStaticObjectProvider(objDef))
                {
                    DynamicObjectProvider dynObject = new DynamicObjectProvider()
                    {
                        Adapter = this, Id = GetDynamicProviderId(objDef), DisplayName = objDef.RootDefinition.DisplayName, Name = CRM2011AdapterUtilities.GetObjectProviderName(objDef)
                    };
                    this.providers.Add(dynObject);
                }
            }

            return(this.providers);
        }