Hi, i need to retrieve records in CRM Online through plugin which registered in CRM 365 On-Premise. This record will be used to fill some fields in CRM 365 On-Premise.
I have created console application that retrieve records in CRM. In the Console Application, i used ClientCredentials to connect with CRM. And it's success to retrieve some records in dynamics CRM.
Something like this:
ClientCredentials credit = new ClientCredentials(); credit.UserName.UserName = "crmadmin"; credit.UserName.Password = "password"; service = new OrganizationServiceProxy(new Uri("mycompany/.../Organization.svc"), null, credit, null);
I use those code inside my plugin. So my plugin looks like:
public void Execute(IServiceProvider serviceprovider) { IPluginExecutionContext context = (IPluginExecutionContext)serviceprovider.GetService(typeof(IPluginExecutionContext)); IOrganizationServiceFactory servicefactory = (IOrganizationServiceFactory)serviceprovider.GetService(typeof(IOrganizationServiceFactory)); service = servicefactory.CreateOrganizationService(context.UserId); if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity) { Entity ent = (Entity)context.InputParameters["Target"]; if (ent.LogicalName != "opportunity") return; //Getting PresalesId of the records //Presales ID on crm 365 will be used for parameters while retrieve records in crm online string Presid = ent.GetAttributeValue<String>("new_presalesid"); try { ClientCredentials credentials = new ClientCredentials(); credentials.UserName.UserName = "Jaya@mlptcare.onmicrosoft.com"; credentials.UserName.Password = "CrmAdmin365"; ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; }; service = new OrganizationServiceProxy(new Uri( "https://mlptcare.api.crm5.dynamics.com/XRMServices/2011/Organization.svc"), null, credentials, null); QueryExpression qxc = new QueryExpression { EntityName = "opportunity", ColumnSet = new ColumnSet(true) }; qxc.Criteria.AddCondition("new_presalesid", ConditionOperator.Equal, Presid); qxc.ColumnSet = new ColumnSet(true); EntityCollection colect = service.RetrieveMultiple(qxc); foreach (Entity e in colect.Entities) { topic = e.GetAttributeValue<String>("name"); estClose = e.GetAttributeValue<DateTime>("estimatedclosedate"); estRev = e.GetAttributeValue<Money>("estimatedvalue"); rating = e.GetAttributeValue<String>("new_rating"); recurring = e.GetAttributeValue<String>("new_recurring"); dealReg = e.GetAttributeValue<String>("new_dealregistration"); perGP = e.GetAttributeValue<Decimal>("new_percentgp"); optyNum = e.GetAttributeValue<Decimal>("new_optynumber"); amName = e.GetAttributeValue<String>("new_amname"); } string mix = topic + estClose + estRev + rating + recurring; throw new InvalidPluginExecutionException("Result of Quert : " + mix); } catch (Exception ex) { throw new InvalidPluginExecutionException(ex.Message); }
After registered this plugin using plugin registration tool. On create Message, Isolation mode: None. I tried to make a new record on the CRM 365. But then comes an error message:
Metadata contains a reference that cannot be resolved
I'm sure that i can access this link
"mlptoffice.api.crm5.dynamics.com/.../Organization.svc"
also i used the SDK for CRM 365 version 8.2
If you ever retrieve record in other crm through plugin. Could you give me some advice how to do that ? Some code snippet would be better.
Thanks
Edited
I tried to use simplified connection using xrm.tooling. It's still failed but with different error message:
Could not load file or assembly 'Microsoft.Xrm.Tooling.Connector, Version=2.2.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.
string connstring = @"Url=https://office.crm5.dynamics.com; Username=admin@offiice.onmicrosoft.com; Password=office; authtype=Office365"; CrmServiceClient conn = new Microsoft.Xrm.Tooling.Connector.CrmServiceClient(connstring); _service = (IOrganizationService)conn.OrganizationWebProxyClient != null ? (IOrganizationService)conn.OrganizationWebProxyClient : (IOrganizationService)conn.OrganizationServiceProxy;
And once again, when tested this on console application, its success to connect with CRM Online. But how to connect with other CRM through Plugin ??
*This post is locked for comments