time 
设为首页】【收藏本站
当前位置: 主页 > ERP > CRM > 【Dynamics CRM插件开发教程】7、Create类型插件实例之四

【Dynamics CRM插件开发教程】7、Create类型插件实例之四

时间:2014-10-17 16:08 点击:464次 字体:[ ]




 

一般情况下,我先增加如下两行代码:

            IPluginExecutionContext context = localContext.PluginExecutionContext;
            IOrganizationService service = localContext.OrganizationService;

前面一句用来获取插件的上下文,后面一句用来获取组织服务。

【Dynamics CRM插件开发教程】7、Create类型插件实例之四_www.fengfly.com

 

代码需要添加 using Microsoft.Xrm.Sdk.Query;,主要使用了如下代码:

            if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
            {
                Entity entity = (Entity)context.InputParameters["Target"];
                string fetchXml = string.Format(@"<fetch mapping='logical'>
                                            <entity name='new_autonumber'><all-attributes/>
                                                <filter type='and'>
                                                        <condition attribute='new_name' operator='eq' value='{0}'" + " /></filter></entity></fetch>",entity.LogicalName);
                System.Threading.Mutex mtx = null;
                try
                {
                    string mutextName = string.Format("{0}{1}", entity.LogicalName, "Autonumber");
                    mtx = new System.Threading.Mutex(false, mutextName);
                    mtx.WaitOne();
                    EntityCollection result = service.RetrieveMultiple(new FetchExpression(fetchXml));
                    string nextIncrementNumber = string.Empty;
                    if (result.Entities.Count == 1)
                    {
                        Entity autoNumber = result.Entities[0];
                        if (!autoNumber.Attributes.Contains("new_counter"))
                            throw new InvalidPluginExecutionException("自动编号记录的当前计数必须包含值!");
                        if (!autoNumber.Attributes.Contains("new_increment"))
                            throw new InvalidPluginExecutionException("自动编号记录的增量必须包含值!");

                        int counter = Int32.Parse(autoNumber.Attributes["new_counter"].ToString());
                        int incrementUnit = Int32.Parse(autoNumber.Attributes["new_increment"].ToString());
                        string prefix = autoNumber.Attributes.Contains("new_prefix") ? autoNumber.Attributes["new_prefix"].ToString() : string.Empty;
                        string prefixSeparator = autoNumber.Attributes.Contains("new_prefixseparator") ? autoNumber.Attributes["new_prefixseparator"].ToString() : string.Empty;
                        string suffix = autoNumber.Attributes.Contains("new_suffix") ? autoNumber.Attributes["new_suffix"].ToString() : string.Empty;
                        string suffixseparator = autoNumber.Attributes.Contains("new_suffixseparator") ? autoNumber.Attributes["new_suffixseparator"].ToString() : string.Empty;
                        string numberFormatter = autoNumber.Attributes.Contains("new_formatter") ? autoNumber.Attributes["new_formatter"].ToString() : string.Empty;
                        string fieldToUpdate;
                        if (autoNumber.Attributes.Contains("new_field"))
                            fieldToUpdate = autoNumber.Attributes["new_field"].ToString();
                        else
                            throw new InvalidPluginExecutionException("自动编号的自动编号字段必须包含值!");
                        nextIncrementNumber = BuildAutoNumber(prefix, prefixSeparator,
                            suffix, suffixseparator, counter, incrementUnit, numberFormatter);
                        //用户没有为自动编号字段输入值
                        if (!entity.Attributes.Contains(fieldToUpdate))
                        {
                            entity.Attributes[fieldToUpdate] = nextIncrementNumber;
                            autoNumber.Attributes["new_counter"] = counter + incrementUnit;
                            service.Update(autoNumber);
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw new InvalidPluginExecutionException("执行插件时出现错误.", ex);
                }
                finally
                {
                    if (mtx != null)
                    {
                        mtx.ReleaseMutex();
                        mtx = null;
                    }
                }

 

上述代码用到了一个方法,方法代码如下:

        private string BuildAutoNumber(string prefix, string prefixSeparator, string suffix, string suffixSeparator, int counter, int incrementUnit, string numberFormatter)
        {
            bool hasPrefix = false, hasSuffix = false;
            string returnNumber = string.Empty;
            if (!string.IsNullOrEmpty(prefix))
            {
                hasPrefix = true;
            }
            if (!string.IsNullOrEmpty(suffix))
            {
                hasSuffix = true;
            }
            counter = counter + incrementUnit;
            returnNumber = (hasPrefix ? prefix + prefixSeparator : "") + counter.ToString(numberFormatter) + (hasSuffix ? suffix + suffixSeparator : "");
            return returnNumber;
        }

代码我不做详细解释了,很快就到部署步骤了。在这之前打开CrmPackage项目中的RegisterFile.crmregister,找到IsolationMode="Sandbox" 这行。

【Dynamics CRM插件开发教程】7、Create类型插件实例之四_www.fengfly.com

 

将其属性值改成None改成如下所示,然后保存此文件并关闭它,这是不将插件部署到沙盒中。这种插件只能部署到On-Primse的CRM中,不能部署到online中。而且部署这种类型的插件需要打开Visual Studio的用户用于部署管理员角色,否则的话会报错。

【Dynamics CRM插件开发教程】7、Create类型插件实例之四_www.fengfly.com

 在最后一个部署步骤之前,还需要按照前面为 Plugins项目前面的方法为 Workflow项目也签名。

【Dynamics CRM插件开发教程】7、Create类型插件实例之四_www.fengfly.com

 

现在终于轮到部署了,右击CrmPackage项目,选择 部署。

【Dynamics CRM插件开发教程】7、Create类型插件实例之四_www.fengfly.com
 
一会儿左下角就出现 部署已成功 字样,这样就算部署成功了。
 
【Dynamics CRM插件开发教程】7、Create类型插件实例之四_www.fengfly.com 



本文地址 : http://www.fengfly.com/plus/view-215419-1.html
标签: Dynamics CRM 插件开发 Create类型
------分隔线----------------------------
最新评论 查看所有评论
发表评论 查看所有评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
验证码: