示例#1
0
        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            WorkAsync(new WorkAsyncInfo()
            {
                Message = $"Generating Early-Bound Classes",
                Work    = (worker, args) =>
                {
                    string dir                               = Path.GetDirectoryName(typeof(MyPluginControl).Assembly.Location).ToLower();
                    string folder                            = Path.GetFileNameWithoutExtension(typeof(MyPluginControl).Assembly.Location);
                    dir                                      = Path.Combine(dir, folder);
                    Process process                          = new Process();
                    var connectionString                     = ConnectionDetail.GetConnectionStringWithPassword();
                    process.StartInfo.Arguments              = "/connectionstring:" + connectionString + " /codewriterfilter:AlbanianXrm.CrmSvcUtilExtensions.FilteringService,AlbanianXrm.CrmSvcUtilExtensions /out:Test.cs ";
                    process.StartInfo.WorkingDirectory       = dir;
                    process.StartInfo.UseShellExecute        = false;
                    process.StartInfo.CreateNoWindow         = true;
                    process.StartInfo.RedirectStandardOutput = true;
                    process.StartInfo.RedirectStandardError  = true;

                    List <string> entities         = new List <string>();
                    List <string> allAttributes    = new List <string>();
                    List <string> allRelationships = new List <string>();

                    foreach (TreeNodeAdv entity in treeViewAdv1.Nodes)
                    {
                        if (entity.CheckState != CheckState.Unchecked)
                        {
                            EntityMetadata metadata = (EntityMetadata)entity.Tag;
                            entities.Add(metadata.LogicalName);
                            foreach (TreeNodeAdv item in entity.Nodes)
                            {
                                if (item.Text == "Attributes")
                                {
                                    if (item.CheckState == CheckState.Checked)
                                    {
                                        allAttributes.Add(metadata.LogicalName);
                                    }
                                    else if (item.CheckState == CheckState.Indeterminate)
                                    {
                                        List <string> attributes = new List <string>();
                                        foreach (TreeNodeAdv attribute in item.Nodes)
                                        {
                                            if (attribute.Checked)
                                            {
                                                var attributeMetadata = (AttributeMetadata)attribute.Tag;
                                                attributes.Add(attributeMetadata.LogicalName);
                                            }
                                        }
                                        process.StartInfo.EnvironmentVariables.Add("AlbanianXrm.EarlyBound:Attributes:" + metadata.LogicalName, string.Join(",", attributes));
                                    }
                                }
                                else if (item.Text == "Relationships")
                                {
                                    if (item.CheckState == CheckState.Checked)
                                    {
                                        allRelationships.Add(metadata.LogicalName);
                                    }
                                    else if (item.CheckState == CheckState.Indeterminate)
                                    {
                                        List <string> relationships = new List <string>();
                                        foreach (TreeNodeAdv relationship in item.Nodes)
                                        {
                                            if (relationship.Checked)
                                            {
                                                var relationshipMetadata = (RelationshipMetadataBase)relationship.Tag;
                                                relationships.Add(relationshipMetadata.SchemaName);
                                            }
                                        }
                                        process.StartInfo.EnvironmentVariables.Add("AlbanianXrm.EarlyBound:Relationships:" + metadata.LogicalName, string.Join(",", relationships));
                                    }
                                }
                            }
                        }
                    }

                    if (entities.Any())
                    {
                        process.StartInfo.EnvironmentVariables.Add("AlbanianXrm.EarlyBound:Entities", string.Join(",", entities));
                    }
                    if (allAttributes.Any())
                    {
                        process.StartInfo.EnvironmentVariables.Add("AlbanianXrm.EarlyBound:AllAttributes", string.Join(",", allAttributes));
                    }
                    if (allRelationships.Any())
                    {
                        process.StartInfo.EnvironmentVariables.Add("AlbanianXrm.EarlyBound:AllRelationships", string.Join(",", allRelationships));
                    }
                    process.EnableRaisingEvents = true;
                    process.StartInfo.FileName  = Path.Combine(dir, "CrmSvcUtil.exe");
                    process.Start();
                    process.WaitForExit();
                    args.Result = process.StandardOutput.ReadToEnd();
                },
                PostWorkCallBack = (args) =>
                {
                    if (args.Error != null)
                    {
                        MessageBox.Show(args.Error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    MessageBox.Show((string)args.Result);
                }
            });
        }