通过 CRM 自定义插件从 table 个项目中查询项目编号列表

Query a list of Project numbers from a table of Projects via CRM Custom Plugin

我在 CRM 中有一个 table 个项目实体,每个项目实体都有一个名为 "project number" 的文本字段。我想查询 table.

中所有可用项目编号的列表

我看过的所有来源都提到我需要使用 ServiceContextXrmServiceContext(),但似乎这些是使用 CrmSvcUtil 工具。我在这部分使用的教程是 here.

根据我过去的 CRM 插件开发经验,,因此使用 CrmSvcUtil 工具与此冲突。

我是不是完全错误地处理了这种情况?我可以访问 OrganizationServiceContext,但我不确定这是否可以让我访问我的项目实体。

编辑:
下面列出了我的参考资料,但找不到 LocalPluginContext。快速 google 搜索建议我只从 sdk 添加项目,但我已经添加了所有内容。

在插件中,您将获得管道的整个执行上下文和组织服务访问权限,以在同一管道中扩展业务功能。

以下这些代码片段是样板代码,将为您提供各种必要的部分,例如用于日志记录的跟踪服务、获取目标实体的上下文、图像等,以及 IOrganizationService 进行更新、检索等服务调用等实现平台扩展

如您所知,插件中将有一个 public class 和一个 public 方法 Execute(IServiceProvider serviceProvider) 我们将使用这个参数 服务提供商

// Obtain the tracing service
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

// Obtain the execution context from the service provider.  
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

// Obtain the organization service reference which you will need for  
// web service calls.  
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

当要从数据库中查询其他项目编号时,使用service.RetrieveMultiple方式查询。您可以通过 fetchxml 查询或使用 queryexpression 来完成。

你可以在网上找到很多例子。 Starter example.

您可以通过两种方式实现这一目标。 1. 不需要上下文的控制台应用程序,而是登录然后获取 IOrganizationService

static void Main(string[] args)
        {
            IOrganizationService organizationService = null;    
            try
            {
                ClientCredentials clientCredentials = new ClientCredentials();
                clientCredentials.UserName.UserName = "AdminCRM@dabc.onmicrosoft.com";
                clientCredentials.UserName.Password = "pwd";

                //For Dynamics 365 Customer Engagement V9.X, set Security Protocol as TLS12
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
                //Get the URL from CRM, Navigate to Settings -> Customizations -> Developer Resources
                //Copy and Paste Organization Service Endpoint Address URL

                organizationService = (IOrganizationService)new OrganizationServiceProxy(new Uri("https:/[OrgUrl]/XRMServices/2011/Organization.svc"),
                    null, clientCredentials, null);

                if (organizationService != null)
                {
                    Guid userid = ((WhoAmIResponse)organizationService.Execute(new WhoAmIRequest())).UserId;

                    if (userid != Guid.Empty)
                    {
                        Console.WriteLine("Connection Established Successfully...");                          
                    FetchXmlTestQuery(organizationService);
                    queryExpressionTest(organizationService);    

                    }
                }
                else
                {
                    Console.WriteLine("Failed to Established Connection!!!");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception caught - " + ex.Message);
            }
            Console.ReadKey();    

        }

 private static void queryExpressionTest(IOrganizationService organizationService)
        {
            QueryExpression qe = new QueryExpression();
            qe.EntityName = "account";
            qe.ColumnSet= new ColumnSet("name", "accountnumber");

            EntityCollection coll = organizationService.RetrieveMultiple(qe);
            foreach (Entity acunt in coll.Entities)
            {
                Console.WriteLine("Name of Account: " + acunt.GetAttributeValue<string>("name"));
                Console.WriteLine("Number of Account: " + acunt.GetAttributeValue<string>("accountnumber"));
            }

        }


private static void FetchXmlTestQuery(IOrganizationService CrmConn)
        {
            // Retrieve all accounts owned by the user with read access rights to the accounts and   
            // where the last name of the user is not Cannon.   
            string fetch = @"  
   <fetch>
  <entity name='account' >
    <attribute name='name' />
<attribute name='accountnumber' />
    <link-entity name='contact' from='parentcustomerid' to='accountid' link-type='inner' alias='Contact' >
      <attribute name='fullname' alias = 'Contact.Fullname' />
    </link-entity>
  </entity>
</fetch> ";

           EntityCollection Coll = CrmConn.RetrieveMultiple(new FetchExpression(fetch));

                foreach (Entity acunt in Coll.Entities)
                {
                    Console.WriteLine("Name of Account: " + acunt.GetAttributeValue<string>("name"));
                    Console.WriteLine("Name of Contact: "  + acunt.GetAttributeValue<AliasedValue>("Contact.Fullname").Value);
                    Console.WriteLine("Number of Account: " + acunt.GetAttributeValue<string>("accountnumber"));
            }


        }

现在您还可以使用 Plugin Context

protected override void ExecuteCrmPlugin(LocalPluginContext localContext)
        {
            if (localContext == null)
            {
                throw new ArgumentNullException("localContext");
            }

            // TODO: Implement your custom plug-in business logic.
            IPluginExecutionContext context = localContext.PluginExecutionContext;
            ITracingService tracingService = localContext.TracingService;
            IOrganizationService orgService = localContext.OrganizationService;

            FetchXmlTestQuery(orgService);
            queryExpressionTest(orgService);
}

 private void FetchXmlTestQuery(IOrganizationService orgService)
        {
            // Retrieve all accounts owned by the user with read access rights to the accounts and   
            // where the last name of the user is not Cannon.   
            string fetch = @"  
   <fetch>
  <entity name='account' >
    <attribute name='name' />
<attribute name='accountnumber' />
    <link-entity name='contact' from='parentcustomerid' to='accountid' link-type='inner' alias='Contact' >
      <attribute name='fullname' alias = 'Contact.Fullname' />
    </link-entity>
  </entity>
</fetch> ";

            EntityCollection Coll = orgService.RetrieveMultiple(new FetchExpression(fetch));

            foreach (Entity acunt in Coll.Entities)
            {
              string accountname= acunt.GetAttributeValue<string>("name");
             string accountnr=  acunt.GetAttributeValue<string>("accountnumber");
            }
        }
        private static void queryExpressionTest(IOrganizationService organizationService)
        {
            QueryExpression qe = new QueryExpression();
            qe.EntityName = "account";
            qe.ColumnSet = new ColumnSet("name", "accountnumber");

            EntityCollection coll = organizationService.RetrieveMultiple(qe);
            foreach (Entity acunt in coll.Entities)
            {
                string accountname = acunt.GetAttributeValue<string>("name");
                string accountnr =  acunt.GetAttributeValue<string>("accountnumber");
            }

        }

这是我要推荐的。

  1. 安装XrmToolBox(确保在解压缩之前取消阻止 zip 文件)
  2. 从工具内的 XrmToolBox 插件商店安装 Early Bound Generator 和 Visual Studio Solution Accelerator。
  3. 运行 Visual Studio 解决方案加速器要么将核心项目添加到您现有的解决方案中,要么使用它来创建新的解决方案。我建议添加示例插件,以便您了解如何创建插件。还建议使用 EarlyBinding。
  4. 运行 EarlyBoundGenerator,粘贴从 Visual Studio 解决方案加速器复制到剪贴板的路径。添加插件所需的任何自定义实体。创建全部。
  5. 在插件项目中创建您的插件。