简体   繁体   English

如何通过Dynamics CRM 365内部部署中的插件在CRM Online中检索记录

[英]How to retrieve records in CRM Online through plugin in Dynamics CRM 365 On-Premise

i need to create plugin which retrieve records in CRM Online. 我需要创建插件来检索CRM Online中的记录。 This plugin will be registered on Dynamics CRM 365 On-Premise. 该插件将在Dynamics CRM 365内部部署中注册。 I've trying all i know to create this plugin, also searching the tutorial on the internet. 我已经尽我所能创建了这个插件,还在互联网上搜索了该教程。 But, everytime i tested the plugin. 但是,每次我测试插件时。 It's always says 总是说

"Metadata contains reference that cannot be resolved https://office.api.crm5.dynamics.com/XRMServices/2011/Organization.svc " . “元数据包含无法解析的引用https://office.api.crm5.dynamics.com/XRMServices/2011/Organization.svc

After getting this error for the first time, i check wether the link above is searcable inside my pc. 第一次收到此错误后,我检查是否上面的链接在我的PC内是可搜索的。 And i'm sure that the link is searchable. 而且我确定该链接是可搜索的。 To connect CRM Online through CRM 365 plugin. 通过CRM 365插件连接CRM Online。 I use this code: 我使用以下代码:

 private static void ConnectToMSCRM()
    {
        try
        {

            ClientCredentials credentials = new ClientCredentials();
            credentials.UserName.UserName = "admin@office.onmicrosoft.com";
            credentials.UserName.Password = "crmpass";
            ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };
            Uri serviceUri = new Uri("https://office.api.crm5.dynamics.com/XRMServices/2011/Organization.svc");
            OrganizationServiceProxy proxy = new OrganizationServiceProxy(serviceUri, null, credentials, null);
            proxy.EnableProxyTypes();

And on the other side, i write this to check if i can connect to CRM online or not: 另一方面,我编写此代码来检查是否可以在线连接到CRM:

IPluginExecutionContext context = (IPluginExecutionContext)serprov.GetService(typeof(IPluginExecutionContext));
        IOrganizationServiceFactory serfac = (IOrganizationServiceFactory)serprov.GetService(typeof(IOrganizationServiceFactory));
        IOrganizationService service = serfac.CreateOrganizationService(context.UserId);

        if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
        {
            Entity ent = (Entity)context.InputParameters["Target"];

           // presaledid = ent.GetAttributeValue<String>("new_presalesid");
            try
            {
                ConnectToMSCRM();
            }
            catch (Exception ex)
            {
                throw new InvalidPluginExecutionException(ex.Message);
            } 

            Guid userid = ((WhoAmIResponse)_service.Execute(new WhoAmIRequest())).UserId;
            if (userid != Guid.Empty)
                return;

Those code returning error message. 这些代码返回错误消息。 Meta data contain references that cannot be resolved. 元数据包含无法解析的引用。 Strangely, when i use the same code inside console application, i'm able to connect with CRM Online. 奇怪的是,当我在控制台应用程序中使用相同的代码时,我可以与CRM Online连接。 I am a bit frustation with this. 我对此有些沮丧。 Almost 5 days still dont get the solution. 仍有近5天没有得到解决方案。 If you have an advice for me to fix the error. 如果您有建议给我解决错误。 Please tell me :) 请告诉我 :)

Also i have another condition while tested this plugin. 另外,我在测试此插件时还有其他条件。 I am on the client office. 我在客户办公室。 To access CRM 365 On-Premise, they provide me an username and password to connect the Wi-fi. 要访问CRM 365内部部署,他们为我提供了用于连接Wi-fi的用户名和密码。 While connecting this wi-fi. 同时连接此Wi-Fi。 I am able to access CRM 365 On-premise. 我能够访问CRM 365内部部署。 But i'm unable to connect CRM Online (no internet access). 但是我无法连接CRM Online(无法访问互联网)。 Meanwhile, inside my plugin code, i must connect to CRM Online to get the records. 同时,在我的插件代码中,我必须连接到CRM Online以获得记录。 Because when i success to connect using console application, i use my personal wifi with internet connection. 因为当我成功使用控制台应用程序进行连接时,我将使用个人wifi和Internet连接。 Is that the source of this problem ? 这是这个问题的根源吗?

If I understand correctly, you say that the on-prem CRM service doesn't have internet access. 如果我理解正确,则说明您的本地CRM服务无法访问互联网。 In that case how can it access CRM Online? 在那种情况下,它如何访问CRM Online?

Also I suggest using the Simplified Connection ( https://msdn.microsoft.com/library/gg695810(v=crm.7).aspx ) from the Microsoft.Xrm.Client library, or XRM Tooling ( https://msdn.microsoft.com/en-us/library/mt608573.aspx ) if you're using CRM 2016. There are a lot of different authentication scenarios. 我也建议使用Microsoft.Xrm.Client库中的简化连接( https://msdn.microsoft.com/library/gg695810(v=crm.7).aspx )或XRM工具( https:// msdn。 microsoft.com/en-us/library/mt608573.aspx )(如果您使用的是CRM 2016)。有很多不同的身份验证方案。 Connecting to CRM online is a whole chain of authentications, tokens and redirects. 在线连接到CRM是一整套认证,令牌和重定向的链。 These tools handle all that, with you just providing a simple connection string. 这些工具只需提供简单的连接字符串即可处理所有这些。

Edit: Example code (working on CRM 2016 on-prem, talking to CRM online). 编辑:示例代码(在CRM 2016本地上工作,在线与CRM交谈)。

In order to use the simplified connection Client.dll, you either need to put it into the server bin (what i did for a quick test) or ILMerge the plugin with the library. 为了使用简化的连接Client.dll,您需要将其放入服务器容器(我为进行快速测试所做的事情),或者将插件与库进行ILMerge。 Have registered this sample plugin on account create, but could be anything. 已在帐户创建中注册了此示例插件,但可以是任何东西。 Correctly got the UserId from the remote (CRM Online) system. 从远程(CRM在线)系统正确获取了UserId。

public class TestOnlinePlugin : IPlugin
{
    public void Execute(IServiceProvider serviceProvider)
    {
        CrmConnection conn = CrmConnection.Parse("Url=https://***.crm4.dynamics.com; Username=***; Password=***;");
        IOrganizationService orgService = new OrganizationService(conn);

        WhoAmIRequest req = new WhoAmIRequest();
        WhoAmIResponse resp = (WhoAmIResponse) orgService.Execute(req);

        throw new InvalidPluginExecutionException($"Remote user ID: {resp.UserId}");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM