public OrganizationResponse Execute(OrganizationRequest request)
 {
     return(instance.Execute(request));
 }
示例#2
0
        public static string GetPickListText(string entityName, string attributeName, int optionSetValue, Microsoft.Xrm.Sdk.IOrganizationService service)
        {
            string AttributeName     = attributeName;
            string EntityLogicalName = entityName;
            RetrieveEntityRequest retrieveDetails = new RetrieveEntityRequest
            {
                EntityFilters = EntityFilters.All,
                LogicalName   = EntityLogicalName
            };
            RetrieveEntityResponse retrieveEntityResponseObj = (RetrieveEntityResponse)service.Execute(retrieveDetails);

            Microsoft.Xrm.Sdk.Metadata.EntityMetadata            metadata         = retrieveEntityResponseObj.EntityMetadata;
            Microsoft.Xrm.Sdk.Metadata.PicklistAttributeMetadata picklistMetadata = metadata.Attributes.FirstOrDefault(attribute => String.Equals(attribute.LogicalName, attributeName, StringComparison.OrdinalIgnoreCase)) as Microsoft.Xrm.Sdk.Metadata.PicklistAttributeMetadata;
            Microsoft.Xrm.Sdk.Metadata.OptionSetMetadata         options          = picklistMetadata.OptionSet;
            IList <OptionMetadata> picklistOption = (from o in options.Options
                                                     where o.Value.Value == optionSetValue
                                                     select o).ToList();
            string picklistLabel = (picklistOption.First()).Label.UserLocalizedLabel.Label;

            return(picklistLabel);
        }
示例#3
0
        public object Resolve(TypeCache type, Microsoft.Xrm.Sdk.IOrganizationService orgService)
        {
            try
            {
                if (services.ContainsKey(type.ObjectInstanceKey))
                {
                    return(services[type.ObjectInstanceKey]);
                }

                lock (locks)
                {
                    if (services.ContainsKey(type.ObjectInstanceKey))
                    {
                        return(services[type.ObjectInstanceKey]);
                    }

                    if (type.FromType == typeof(Guid) && type.Name != null && type.Name.ToLower() == nameof(this.pluginExecutionContext.PrimaryEntityId).ToLower())
                    {
                        return(this.pluginExecutionContext.PrimaryEntityId);
                    }

                    if (type.FromType == typeof(string) && type.Name != null && type.Name.ToLower() == nameof(this.pluginExecutionContext.PrimaryEntityName).ToLower())
                    {
                        return(this.pluginExecutionContext.PrimaryEntityName);
                    }

                    if (type.FromType == typeof(string) && type.Name != null && type.Name.ToLower() == nameof(BasePlugin.UnsecureConfig).ToLower())
                    {
                        return(this.UnsecureConfig);
                    }

                    if (type.FromType == typeof(string) && type.Name != null && type.Name.ToLower() == nameof(BasePlugin.SecureConfig).ToLower())
                    {
                        return(this.SecureConfig);
                    }

                    if (type.FromType == typeof(Microsoft.Xrm.Sdk.Query.QueryExpression))
                    {
                        if (pluginExecutionContext.InputParameters.Contains("Query"))
                        {
                            var query = pluginExecutionContext.InputParameters["Query"];
                            if (query is Microsoft.Xrm.Sdk.Query.QueryExpression qe)
                            {
                                // no cache by design - need to be resolved on all request.
                                return(qe);
                            }

                            if (query is Microsoft.Xrm.Sdk.Query.FetchExpression fe)
                            {
                                var resp = (Microsoft.Crm.Sdk.Messages.FetchXmlToQueryExpressionResponse)orgService.Execute(new Microsoft.Crm.Sdk.Messages.FetchXmlToQueryExpressionRequest
                                {
                                    FetchXml = fe.Query
                                });
                                return(resp.Query);
                            }

                            throw new Exceptions.UnresolvedQueryParameter(type.Name);
                        }
                        else
                        {
                            throw new InvalidPluginExecutionException("QueryExpression can only be requested for RetrieveMultiple requests");
                        }
                    }

                    if (type.IsTarget && !type.IsReference)
                    {
                        var entity = (Microsoft.Xrm.Sdk.Entity)pluginExecutionContext.InputParameters["Target"];
                        services[type.ObjectInstanceKey] = Extensions.Sdk.KiponSdkGeneratedExtensionMethods.ToEarlyBoundEntity(entity);
                        return(services[type.ObjectInstanceKey]);
                    }

                    if (type.IsPreimage)
                    {
                        var imgName = PluginMethod.ImageSuffixFor(1, pluginExecutionContext.Stage, pluginExecutionContext.Mode == 1);
                        var entity  = (Microsoft.Xrm.Sdk.Entity)pluginExecutionContext.PreEntityImages[imgName];
                        services[type.ObjectInstanceKey] = Extensions.Sdk.KiponSdkGeneratedExtensionMethods.ToEarlyBoundEntity(entity);
                        return(services[type.ObjectInstanceKey]);
                    }

                    if (type.IsPostimage)
                    {
                        var imgName = PluginMethod.ImageSuffixFor(2, pluginExecutionContext.Stage, pluginExecutionContext.Mode == 1);
                        var entity  = (Microsoft.Xrm.Sdk.Entity)pluginExecutionContext.PostEntityImages[$"postimage{imgName}"];
                        services[type.ObjectInstanceKey] = Extensions.Sdk.KiponSdkGeneratedExtensionMethods.ToEarlyBoundEntity(entity);
                        return(services[type.ObjectInstanceKey]);
                    }

                    if (type.IsMergedimage)
                    {
                        var merged = new Microsoft.Xrm.Sdk.Entity();

                        var target = pluginExecutionContext.InputParameters["Target"] as Microsoft.Xrm.Sdk.Entity;

                        if (target != null)
                        {
                            merged.Id          = target.Id;
                            merged.LogicalName = target.LogicalName;
                        }
                        else
                        {
                            var er = pluginExecutionContext.InputParameters["Target"] as Microsoft.Xrm.Sdk.EntityReference;
                            merged.Id          = er.Id;
                            merged.LogicalName = er.LogicalName;
                        }

                        if (pluginExecutionContext.MessageName == "Create")
                        {
                            merged = target;
                        }
                        else
                        {
                            var imgName = PluginMethod.ImageSuffixFor(1, pluginExecutionContext.Stage, pluginExecutionContext.Mode == 1);
                            var pre     = (Microsoft.Xrm.Sdk.Entity)pluginExecutionContext.PreEntityImages[imgName];

                            foreach (var attr in pre.Attributes.Keys)
                            {
                                merged[attr] = pre[attr];
                            }

                            if (target != null)
                            {
                                foreach (var attr in target.Attributes.Keys)
                                {
                                    merged[attr] = target[attr];
                                }
                            }
                        }

                        services[type.ObjectInstanceKey] = Extensions.Sdk.KiponSdkGeneratedExtensionMethods.ToEarlyBoundEntity(merged);
                        return(services[type.ObjectInstanceKey]);
                    }

                    if (type.IsReference)
                    {
                        var target = (Microsoft.Xrm.Sdk.EntityReference)pluginExecutionContext.InputParameters["Target"];
                        if (type.Constructor != null)
                        {
                            services[type.ObjectInstanceKey] = type.Constructor.Invoke(new object[] { target });
                        }
                        else
                        {
                            services[type.ObjectInstanceKey] = target;
                        }
                        return(services[type.ObjectInstanceKey]);
                    }

                    if (type.FromType == typeof(Microsoft.Xrm.Sdk.IOrganizationService))
                    {
                        return(this.GetOrganizationService(type.RequireAdminService));
                    }

                    if (type.IsQuery)
                    {
                        var uow = this.GetIUnitOfWork(type.RequireAdminService);
                        var repositoryProperty = type.RepositoryProperty;
                        var repository         = repositoryProperty.GetValue(uow, new object[0]);
                        var queryMethod        = type.QueryMethod;
                        return(queryMethod.Invoke(repository, new object[0]));
                    }

                    if (type.IsRepository)
                    {
                        var uow           = this.GetIUnitOfWork(type.RequireAdminService);
                        var queryProperty = type.RepositoryProperty;
                        return(queryProperty.GetValue(uow, new object[0]));
                    }

                    if (type.IsEntityCache)
                    {
                        var uow           = this.GetIUnitOfWork(type.RequireAdminService);
                        var cacheProperty = uow.GetType().GetProperty("Cache");
                        return(cacheProperty.GetValue(uow, new object[0]));
                    }

                    if (type.FromType == typeof(Guid))
                    {
                        if (type.Name.ToLower() == "id")
                        {
                            return(pluginExecutionContext.PrimaryEntityId);
                        }

                        if (type.Name.ToLower() == "listid")
                        {
                            return(pluginExecutionContext.InputParameters["ListId"]);
                        }

                        if (type.Name.ToLower() == "entityid")
                        {
                            return(pluginExecutionContext.InputParameters["EntityId"]);
                        }

                        throw new Exceptions.UnresolveableParameterException(type.FromType, type.Name);
                    }

                    if (type.FromType == typeof(Microsoft.Xrm.Sdk.Relationship))
                    {
                        if (pluginExecutionContext.InputParameters.Contains("Relationship"))
                        {
                            return((Microsoft.Xrm.Sdk.Relationship)pluginExecutionContext.InputParameters["Relationship"]);
                        }

                        throw new InvalidPluginExecutionException("Relationship is requested as input parameter but there is no such information in the payload");
                    }

                    return(this.CreateServiceInstance(type));
                }
            }
            catch (System.Collections.Generic.KeyNotFoundException)
            {
                var typeName = (type?.FromType?.FullName) ?? "type was null";
                var key      = type?.ObjectInstanceKey ?? "object instance key as null";
                throw new InvalidPluginExecutionException($"Unable to resolve service {typeName}, {key} or one of its dependencies.");
            }
        }