简体   繁体   中英

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. This plugin will be registered on Dynamics CRM 365 On-Premise. 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 " .

After getting this error for the first time, i check wether the link above is searcable inside my pc. And i'm sure that the link is searchable. To connect CRM Online through CRM 365 plugin. 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:

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. I am a bit frustation with this. Almost 5 days still dont get the solution. 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. While connecting this wi-fi. I am able to access CRM 365 On-premise. But i'm unable to connect CRM Online (no internet access). Meanwhile, inside my plugin code, i must connect to CRM Online to get the records. Because when i success to connect using console application, i use my personal wifi with internet connection. Is that the source of this problem ?

If I understand correctly, you say that the on-prem CRM service doesn't have internet access. In that case how can it access 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. Connecting to CRM online is a whole chain of authentications, tokens and redirects. 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).

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. Have registered this sample plugin on account create, but could be anything. Correctly got the UserId from the remote (CRM Online) system.

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}");
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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