예제 #1
0
        public void Execute(IServiceProvider serviceProvider)
        {
            //Extract the tracing service for use in debugging sandboxed plug-ins.
            ITracingService tracingService =
                (ITracingService)serviceProvider.GetService(typeof(ITracingService));

            // Obtain the execution context from the service provider.
            IPluginExecutionContext context = (IPluginExecutionContext)
                                              serviceProvider.GetService(typeof(IPluginExecutionContext));
            IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            var service = serviceFactory.CreateOrganizationService(context.UserId);
            OrganizationServiceContext ctx = new OrganizationServiceContext(service);
            FaultException             ex1 = new FaultException();



            if (context.InputParameters.Contains("Target") &&
                context.InputParameters["Target"] is Entity)
            {
                try
                {
                    Entity entity          = (Entity)context.InputParameters["Target"];
                    var    entityName      = entity.LogicalName;
                    var    publisherPrefix = entityName.Split('_')[0];
                    tracingService.Trace("pub prefix {0}", publisherPrefix);
                    QueryExpression qe = new QueryExpression();
                    qe.EntityName = "fp_fieldnamepopulator";
                    qe.ColumnSet  = new ColumnSet();
                    qe.ColumnSet.AddColumns("fp_name", "fp_fieldnames");

                    qe.Criteria.AddCondition("fp_name", ConditionOperator.Equal, entityName.ToString().ToLower());
                    var results = service.RetrieveMultiple(qe);

                    if (results.Entities.ToArray().Count() > 0)
                    {
                        Entity referenceEntity = results.Entities.First();
                        string fieldNames      = referenceEntity.GetAttributeValue <string>("fp_fieldnames");
                        char[] comma           = { ',' };

                        var fieldNameArr = fieldNames.Split(comma).ToList();
                        if (fieldNameArr.Count() > 2)
                        {
                            throw new InvalidPluginExecutionException("You can only add 2 fields", ex1);
                        }

                        string nameToUse;

                        if (publisherPrefix == entityName)
                        {
                            nameToUse = "name";
                        }
                        else
                        {
                            nameToUse = publisherPrefix + "_name";
                        }


                        if (string.IsNullOrWhiteSpace(fieldNames))
                        {
                            throw new InvalidPluginExecutionException("null", ex1);
                        }

                        string refName  = "";
                        string refName1 = "";

                        Entity entityToUse;

                        if (context.MessageName.ToUpper() == "UPDATE")
                        {
                            var columns = new ColumnSet();
                            foreach (string fieldName in fieldNameArr)
                            {
                                string fn = fieldName.Trim();
                                columns.AddColumn(fn);
                            }
                            entityToUse = service.Retrieve(entityName, entity.Id, columns);
                        }
                        else
                        {
                            entityToUse = entity;
                        }
                        foreach (string fieldName in fieldNameArr)
                        {
                            string fn = fieldName.Trim();
                            if (entityToUse.Attributes.Contains(fn))
                            {
                                if (entityToUse.Attributes[fn] is EntityReference)
                                {
                                    tracingService.Trace("pub prefix {0}", publisherPrefix);
                                    var refId = entityToUse.GetAttributeValue <EntityReference>(fn).Id;
                                    tracingService.Trace("ref ID {0}", refId);
                                    var nameToUseOfContext = nameToUse;
                                    if (fn == "contact")
                                    {
                                        fn = "contact";
                                        nameToUseOfContext = "fullname";
                                    }

                                    if (fn == "account")
                                    {
                                        fn = "account";
                                        nameToUseOfContext = "name";
                                    }

                                    var valToUse = service.Retrieve(fn, refId, new ColumnSet(nameToUseOfContext)).GetAttributeValue <string>(nameToUseOfContext);

                                    if (string.IsNullOrEmpty(refName))
                                    {
                                        refName = valToUse;
                                    }
                                    else
                                    {
                                        refName1 = valToUse;
                                    };
                                    fn = fieldName.Trim();
                                }
                                if (entityToUse.Attributes[fn] is string)
                                {
                                    var valToUse = entityToUse.GetAttributeValue <string>(fn);
                                    if (string.IsNullOrEmpty(refName))
                                    {
                                        refName = valToUse;
                                    }
                                    else
                                    {
                                        refName1 = valToUse;
                                    };
                                }
                                if (entityToUse.Attributes[fn] is OptionSetValue)
                                {
                                    var val = new OptionSetHelper();

                                    OptionSetHelper labelFromInput = new OptionSetHelper();
                                    var             valToUse       = labelFromInput.getLabelFromField(entityToUse, fn, service);
                                    //var valToUse = entityToUse.GetAttributeValue<OptionSetValue>(fn).Value.ToString();

                                    //var valToUse1 = entityToUse.FormattedValues[fn];
                                    //throw new InvalidPluginExecutionException(label, ex1);

                                    if (string.IsNullOrEmpty(refName))
                                    {
                                        refName = valToUse;
                                    }
                                    else
                                    {
                                        refName1 = valToUse;
                                    };
                                }
                                if (entityToUse.Attributes[fn] is bool)
                                {
                                    var valToUse = entityToUse.GetAttributeValue <bool>(fn).ToString();
                                    if (string.IsNullOrEmpty(refName))
                                    {
                                        refName = valToUse;
                                    }
                                    else
                                    {
                                        refName1 = valToUse;
                                    };
                                }
                            }
                            else
                            {
                                //add to TRACE log
                                // throw new InvalidPluginExecutionException("none2", ex1);
                                tracingService.Trace("(inside try block) Field Name Populator Helper Plugin: {0}", ex1.ToString());
                            }
                        }
                        MetadataHelper meta        = new MetadataHelper(service, entity);
                        int            fieldLength = meta.getMaxLength(nameToUse).GetValueOrDefault();
                        StringHelper   helper      = new StringHelper();
                        var            newStr      = helper.concatenateAfterCalc(refName, refName1, fieldLength, " ");
                        if (publisherPrefix == entityName)
                        {
                            entity["name"] = newStr;
                        }
                        else
                        {
                            entity[publisherPrefix + "_name"] = newStr;
                        }

                        //  if (context.MessageName.ToUpper() == "UPDATE")
                        //  {
                        if (context.Depth > 1)
                        {
                            return;
                        }
                        service.Update(entity);
                        //  }
                    }
                }
                catch (Exception ex)
                {
                    tracingService.Trace("Field Name Populator Helper Plugin: {0}", ex.ToString());
                    throw;
                }
            }
        }