防止通过插件发送电子邮件和显示消息 [英] Prevent sending email and show message via plug-in

查看:88
本文介绍了防止通过插件发送电子邮件和显示消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用发送" Pre_operation消息在电子邮件"实体中编写crm2011插件.我想要做的是,当我单击电子邮件实体中的发送"按钮时,我在发送之前进行了必要的检查.如果检查不正确,我想阻止并停止发送电子邮件并显示警报消息",并停止第二个插件(此插件发送电子邮件并创建关联的实体以转换案例").请给我一些关于该插件的建议? 我应该使用验证前阶段还是手术前状态?以及如何返回false来停止插件.

I am writing crm2011 plugin in "Email" entity with "Send" Message of Pre_operation. What i want to do is when i click "Send" button in email entity, I do the necessary checking before send. If the checking is not correct, I want to prevent and stop the sending email and show "the alert message" and stop the second plugin(this plugin send email and create the associated entity to convert "Case"). Please give me some suggestion for that plugin? Should i use pre-Validation stage or Pre_operation state? And how can I return false to stop plugin.

  public void Execute(IServiceProvider serviceProvider)
    {
        try
        {
            string message = null;
            _serviceProvider = serviceProvider;
            _context = (IPluginExecutionContext)
                                         serviceProvider.GetService(typeof(IPluginExecutionContext));

            _serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            _currentUser = _context.UserId;
            message = _context.MessageName.ToLower();
            if (message == "send")
            {
                if (_context.InputParameters != null && _context.InputParameters.Contains("EmailId"))
                {
                    object objEmailId = _context.InputParameters["EmailId"];
                    if (objEmailId != null)
                    {
                        _emailId = new Guid(objEmailId.ToString());
                        FindEmailInfo();
                        if (_email != null)
                        {
                            if (_email.Attributes.Contains("description") && _email.Attributes["description"] != null)//Email descritpion is not null
                            {
                                string emaildescription = StripHTML();

                                //Find KB Article prefix no in system config entity
                                serviceguideprefix = "ServiceGuidesPrefix";
                                QueryByAttribute query = new QueryByAttribute("ppp_systemconfig");
                                query.ColumnSet = new ColumnSet(true);
                                query.AddAttributeValue(sysconfig_name, serviceguideprefix);
                                EntityCollection sysconfig = _service.RetrieveMultiple(query);
                                if (sysconfig.Entities.Count > 0)
                                {
                                    Entity e = sysconfig.Entities[0];
                                    if (e.Attributes.Contains("ppp_value"))
                                    {
                                        ppp_value = e.Attributes["ppp_value"].ToString();
                                    }
                                }
                                if (ppp_value != null && ppp_value != string.Empty)
                                {
                                    //var matches = Regex.Matches(emaildescription, @"KBA-\d*-\w*").Cast<Match>().ToArray();
                                    var matches = Regex.Matches(emaildescription, ppp_value + @"-\d*-\w*").Cast<Match>().ToArray();
                                    //ReadKBNo(emaildescription);
                                    foreach (Match kbnumber in matches)
                                    {
                                        EntityCollection kbarticlecol = FindKBArticleIds(kbnumber.ToString());
                                        if (kbarticlecol.Entities.Count > 0)
                                        {
                                            Entity kbariticle = kbarticlecol.Entities[0];
                                            if (kbariticle.Attributes.Contains("mom_internalkm"))
                                            {
                                                bool internalserviceguide = (bool)kbariticle.Attributes["mom_internalkm"];
                                                if (internalserviceguide) found = true;
                                                else found = false;
                                            }
                                            else found = false;
                                        }
                                    }
                                }
                                if (found)
                                {
                                    //-----
                                }
                            }
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            throw new InvalidPluginExecutionException(ex.Message, ex);
        }
    }

推荐答案

停止插件非常简单,只需抛出InvalidPluginException,您将在警报窗口中将显示给您的消息显示给用户.您必须在发送前执行此操作.在这种情况下,我认为它是否经过预验证或预操作并不重要.

Well stopping the plugin is dead easy you just throw InvalidPluginException, the message you give it will be shown to the user in a alert window. You will have to do this on the pre of the send. In this case I don't think it will matter if its pre-validation or pre-operation.

是的,即使代码中没有发生异常,也应该抛出InvalidPluginException.我接受这不是我们通常会做的事情,而是它的工作方式. Msdn具有更多详细信息: http://msdn.microsoft.com/en-us/library/gg334685.aspx

Yes, you should throw an InvalidPluginException even if no exception has happened in code. I accept this isnt what we would normally do, but its the way its meant to work. Msdn has more details: http://msdn.microsoft.com/en-us/library/gg334685.aspx

例如,代码如下所示:

public void Execute(IServiceProvider serviceProvider)
{
    try    
    {
        //This is where we validate the email send
        if(emailIsOkay)
        {
            //Do something
        }
        else if(emailIsNotOkay)
        {
            //Throw and exception that will stop the plugin and the message will be shown to the user (if its synchronous)
            throw new InvalidPluginExecutionException("Hello user, your email is not correct!!");
        }
    }
    catch (InvalidPluginExecutionException invalid)
    {
        //We dont to catch exception for InvalidPluginExecution, so just throw them on
        throw; 
    }
    catch (Exception ex)
    {
        //This exception catches if something goes wrong in the code, or some other process.
        throw new InvalidPluginExecutionException(ex.Message, ex);
    }
}

这篇关于防止通过插件发送电子邮件和显示消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆