Example #1
0
        // credit: https://community.dynamics.com/crm/b/mshelp/archive/2012/06/12/get-optionset-text-from-value-or-value-from-text
        /// <summary>
        ///     This function is used to retrieve the optionset label using the optionset value
        /// </summary>
        /// <param name="service"></param>
        /// <param name="entityName"></param>
        /// <param name="attributeName"></param>
        /// <param name="selectedValue"></param>
        /// <returns></returns>
        internal static string GetOptionsSetTextForValue(IOrganizationService service, string entityName,
                                                         string attributeName, int selectedValue, string orgId)
        {
            var cacheKey = $"GetOptionsSetTextForValue|{entityName}|{attributeName}"
                           + $"|{selectedValue}|{Libraries.Common.Helpers.GetAssemblyName(0)}";
            var attributeCached = CacheHelpers.GetFromMemCache <string>(cacheKey, orgId);

            if (attributeCached != null)
            {
                return(attributeCached);
            }

            var retrieveAttributeRequest =
                new
                RetrieveAttributeRequest
            {
                EntityLogicalName     = entityName,
                LogicalName           = attributeName,
                RetrieveAsIfPublished = true
            };

            // Execute the request.
            var retrieveAttributeResponse = (RetrieveAttributeResponse)service.Execute(retrieveAttributeRequest);

            // Access the retrieved attribute.
            var retrievedPicklistAttributeMetadata = (PicklistAttributeMetadata)retrieveAttributeResponse.AttributeMetadata;
            var optionList = retrievedPicklistAttributeMetadata.OptionSet.Options.ToArray();

            return(CacheHelpers.AddToMemCache(cacheKey,
                                              (from oMD in optionList
                                               where oMD.Value == selectedValue
                                               select oMD.Label.LocalizedLabels[0].Label).FirstOrDefault(),
                                              DateTime.Now.AddHours(12), orgId));
        }
Example #2
0
        internal static IEnhancedOrgService GetConnection(string connectionString, bool noCache = false)
        {
            if (noCache)
            {
                ResetCache(connectionString);
            }

            var memKey = $"{ConnCacheMemKey}_{connectionString}";

            lock (lockObj)
            {
                try
                {
                    var service = CacheHelpers.GetFromMemCache <IEnhancedOrgService>(memKey);

                    if (service != null)
                    {
                        return(service);
                    }

                    Status.Update($"Creating connection to CRM ... ");
                    Status.Update($"Connection String:" + $" '{CrmHelpers.SecureConnectionString(connectionString)}'.");

                    service = EnhancedServiceHelper.GetCachingPoolingService(
                        new ServiceParams
                    {
                        ConnectionParams =
                            new ConnectionParams
                        {
                            ConnectionString = connectionString
                        },
                        PoolParams =
                            new PoolParams
                        {
                            PoolSize       = 5,
                            DequeueTimeout = TimeSpan.FromSeconds(20)
                        },
                        CachingParams = new CachingParams {
                            CacheScope = CacheScope.Service
                        },
                        OperationHistoryLimit = 1
                    });
                    CacheHelpers.AddToMemCache(memKey, service, TimeSpan.MaxValue);

                    Status.Update($"Created connection.");

                    return(service);
                }
                catch
                {
                    CacheHelpers.RemoveFromMemCache(memKey);
                    throw;
                }
            }
        }
Example #3
0
        // credit: http://blogs.msdn.com/b/crm/archive/2007/06/18/understanding-crm-metadata-primarykey-and-primaryfield.aspx
        /// <summary>
        ///     Retrieve a CRM Entity's primarykey and primaryfield
        /// </summary>
        /// <param name="service"></param>
        /// <param name="entity"></param>
        internal static string GetEntityPrimaryFieldValue(IOrganizationService service, EntityReference entity, string orgId)
        {
            var cacheKey     = $"GetEntityPrimaryFieldValue|{entity.LogicalName}|{Libraries.Common.Helpers.GetAssemblyName(0)}";
            var primaryField = CacheHelpers.GetFromMemCache <string>(cacheKey, orgId)
                               ?? CacheHelpers.AddToMemCache(cacheKey, ((RetrieveEntityResponse)service.Execute(
                                                                            new RetrieveEntityRequest
            {
                EntityFilters = EntityFilters.Entity,
                LogicalName = entity.LogicalName
            })).EntityMetadata.PrimaryNameAttribute,
                                                             DateTime.Now.AddHours(12), orgId);

            return(service.Retrieve(entity.LogicalName, entity.Id, new ColumnSet(primaryField))
                   .GetAttributeValue <string>(primaryField));
        }