Usuário com melhor resposta
Preenchimento de campo customizado via Plugin

Pergunta
-
Respostas
-
Olá Bruno,
Veja o exemplo abaixo e note as linhas em [negrito] a qual fazem acesso ao [Web Services] do CRM para busca/alteração de entidades [customizadas]:
P.S.: Para adicionar o WS do CRM em seu projeto de Plugin, veja exemplo no SDK - http://msdn.microsoft.com/en-us/library/cc151015.aspx.
_______________________________________________________
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Crm.Sdk;using crmSDK = PluginTest.wsCrmService;
namespace Plugin___teste
{
public class HelloWorldPlugin : IPlugin
{
public void Execute(IPluginExecutionContext context)
{
crmSDK.CrmAuthenticationToken token = new crmSDK.CrmAuthenticationToken();
token.AuthenticationType = 0; // AD Authentication
token.OrganizationName = "ExemploSA"; // Pegar o nome da organização no [Gerenciador de Implantação do CRM - Menu "Orgsnizações"]crmSDK.CrmService service = new crmSDK.CrmService();
service.CrmAuthenticationTokenValue = token;
service.Credentials = System.Net.CredentialCache.DefaultCredentials;DynamicEntity entity = null;
// Check if the input parameters property bag contains a target
// of the create operation and that target is of type DynamicEntity.
if (context.InputParameters.Properties.Contains("Target") &&
context.InputParameters.Properties["Target"] is DynamicEntity)
{
// Obtain the target business entity from the input parmameters.
entity = (DynamicEntity)context.InputParameters.Properties["Target"];// Verify that the entity represents an account.
if (entity.Name != crmSDK.EntityName.contact.ToString()) { return; }Guid ID = ((Key)entity.Properties["contactid"]).Value;
crmSDK.contact ctt = (crmSDK.contact)service.Retrieve("contact", ID, new crmSDK.AllColumns());// Update [CUSTOM] Field
ctt.new_rg = "SOME VALUE";// Create the target object for the request.
crmSDK.TargetUpdateContact target = new crmSDK.TargetUpdateContact();// Set the properties of the target object.
target.Contact = ctt;// Create the request object.
crmSDK.UpdateRequest update = new crmSDK.UpdateRequest();// Set the properties of the request object.
update.Target = target;// Execute the request.
crmSDK.UpdateResponse updated = null;try
{
updated = (crmSDK.UpdateResponse)service.Execute(update);
}
catch (System.Web.Services.Protocols.SoapException ex)
{
System.Diagnostics.EventLog.WriteEntry("Application", ex.Detail.InnerText);
}}
}
}
}
[]
Gilberto Tezini - MCP ASP.NET (C#/VB.NET) / MCP CRM - (http://gtezini.blogspot.com)- Marcado como Resposta Bruno dos Santos A. Cardoso segunda-feira, 27 de julho de 2009 14:25
Todas as Respostas
-
Olá Bruno,
Veja o exemplo abaixo e note as linhas em [negrito] a qual fazem acesso ao [Web Services] do CRM para busca/alteração de entidades [customizadas]:
P.S.: Para adicionar o WS do CRM em seu projeto de Plugin, veja exemplo no SDK - http://msdn.microsoft.com/en-us/library/cc151015.aspx.
_______________________________________________________
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Crm.Sdk;using crmSDK = PluginTest.wsCrmService;
namespace Plugin___teste
{
public class HelloWorldPlugin : IPlugin
{
public void Execute(IPluginExecutionContext context)
{
crmSDK.CrmAuthenticationToken token = new crmSDK.CrmAuthenticationToken();
token.AuthenticationType = 0; // AD Authentication
token.OrganizationName = "ExemploSA"; // Pegar o nome da organização no [Gerenciador de Implantação do CRM - Menu "Orgsnizações"]crmSDK.CrmService service = new crmSDK.CrmService();
service.CrmAuthenticationTokenValue = token;
service.Credentials = System.Net.CredentialCache.DefaultCredentials;DynamicEntity entity = null;
// Check if the input parameters property bag contains a target
// of the create operation and that target is of type DynamicEntity.
if (context.InputParameters.Properties.Contains("Target") &&
context.InputParameters.Properties["Target"] is DynamicEntity)
{
// Obtain the target business entity from the input parmameters.
entity = (DynamicEntity)context.InputParameters.Properties["Target"];// Verify that the entity represents an account.
if (entity.Name != crmSDK.EntityName.contact.ToString()) { return; }Guid ID = ((Key)entity.Properties["contactid"]).Value;
crmSDK.contact ctt = (crmSDK.contact)service.Retrieve("contact", ID, new crmSDK.AllColumns());// Update [CUSTOM] Field
ctt.new_rg = "SOME VALUE";// Create the target object for the request.
crmSDK.TargetUpdateContact target = new crmSDK.TargetUpdateContact();// Set the properties of the target object.
target.Contact = ctt;// Create the request object.
crmSDK.UpdateRequest update = new crmSDK.UpdateRequest();// Set the properties of the request object.
update.Target = target;// Execute the request.
crmSDK.UpdateResponse updated = null;try
{
updated = (crmSDK.UpdateResponse)service.Execute(update);
}
catch (System.Web.Services.Protocols.SoapException ex)
{
System.Diagnostics.EventLog.WriteEntry("Application", ex.Detail.InnerText);
}}
}
}
}
[]
Gilberto Tezini - MCP ASP.NET (C#/VB.NET) / MCP CRM - (http://gtezini.blogspot.com)- Marcado como Resposta Bruno dos Santos A. Cardoso segunda-feira, 27 de julho de 2009 14:25
-
Gilberto,
Primeiramente obrigado pela resposta.
No entanto está acontecendo um erro no momento da execução que eu não sei o motivo.
Quais poderiam ser as possíveis causas deste erro? Tem alguma forma de testar o plugin passo a passo ou algo do tipo? Porque da forma como está eu não tenho idéia do que pode ser o erro. Só mais uma dúvida: este PluginTest é o nome do Projeto?
Obrigado,
Bruno Cardoso -
Olá Bruno,
Você pode [Depurar] seu plugin, de forma a descobrir o que acontece. Veja como em http://msdn.microsoft.com/en-us/library/cc151088.aspx.
´
P.S.: [PluginTest] é o nome do meu projeto sim.
[]
Gilberto Tezini - MCP ASP.NET (C#/VB.NET) / MCP CRM - (http://gtezini.blogspot.com) -
-
-
-
-
Olá Gilberto,
Resetei o IIS, como vc aconselhou acima, porém o erro persiste.
Qual a diferença em registrar o Plugin em disco, na base e no GAC?
Eu sempre registrava na base, quando tentei registrar em disco, deu o mesmo erro (time out). E quando registrei no GAC deu erro.
Será que este erro pode estar relacionado com a forma de registro?
Como eu disse acima, o Plugin funciona, porém sempre ocorre timeout e eu só consigo ver as alterações posteriormente.
Vc tem mais alguma sugetão?
Obrigado,
Bruno Cardoso