segunda-feira, 21 de novembro de 2011

Customization using Visual Studio 2010 (C #) - Plug-In

Customization using Visual Studio 2010 (C #) - Plug-In

In the post 07. Customization using Visual Studio 2010 (C #) and 08. Customization using Visual Studio 2010 (C #) - Download the SDK, we began the study of customization using VS 2010. Today we will develop the plug-in to record the data from Dynamics CRM to ERP customer.
Launch Visual Studio 2010 and open a new project by adding a class library.

Name the CadCliente, and select the location where you saved the files in your project and click OK.
We need to sign the bill as follows:
Click right-click properties, and click Open.
Check Sign the Assembly and click in Signing and New

In the field (key file name), tell CadCliente and click OK




 
Add a new item in the project of type Class with the name of PluginCliente.cs


And insert the following code:

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using System.Data.SqlClient;

namespace CadCliente
{
public class PluginCliente : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
// Pega contexto em execução
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

if (context.MessageName.ToUpper() == "CREATE")
{
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
// Captura a entidade
Entity targetEntity = (Entity)context.InputParameters["Target"];

if (targetEntity.LogicalName.ToUpper() == "ACCOUNT")
{
// Captura o serviço
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

// String de conexão SQL do banco de dados ERP
string conexao = "Data Source=WIN-4EF7UMIGTTO;Initial Catalog=ERP;Integrated Security=SSPI";

string sql = "Insert into ERP_Cliente (codcliente,nome,endereco,bairro,cidade,estado,cep,telefone) values (@codcliente, @nome, @endereco, @bairro, @cidade, @estado, @cep, @telefone)";
SqlConnection con = new SqlConnection(conexao);
SqlCommand comando = new SqlCommand(sql, con);

comando.Parameters.Add(new SqlParameter("@codcliente", targetEntity["jd_codigo"].ToString()));
comando.Parameters.Add(new SqlParameter("@nome", targetEntity["name"].ToString()));
comando.Parameters.Add(new SqlParameter("@endereco", targetEntity["address1_name"].ToString()));
comando.Parameters.Add(new SqlParameter("@bairro", targetEntity["address1_line1"].ToString()));
comando.Parameters.Add(new SqlParameter("@cidade", targetEntity["address1_city"].ToString()));
comando.Parameters.Add(new SqlParameter("@estado", targetEntity["address1_stateorprovince"].ToString()));
comando.Parameters.Add(new SqlParameter("@cep", targetEntity["address1_postalcode"].ToString()));
comando.Parameters.Add(new SqlParameter("@telefone", targetEntity["telephone1"].ToString()));

try
{
con.Open();
comando.ExecuteNonQuery();
con.Close();
}
catch (Exception ex)
{
return;
}
}
else
{
return;
}
}
else
{
return;
}
}
else if (context.MessageName.ToUpper() == "UPDATE")
{
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
// Captura a entidade.
Entity targetEntity = (Entity)context.InputParameters["Target"];

if (targetEntity.LogicalName.ToUpper() == "ACCOUNT")
{
// Captura o serviço
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

// Id do registro atual
Guid id = (Guid)targetEntity["accountid"];

// captura dados da entidade
Entity entidade = service.Retrieve("account", id, new ColumnSet(true));

// String de conexão SQL do banco de dados ERP
// WIN-4EF7UMIGTTO = Nome da máquina/servidor pode ser substituido por "." (ponto).

string conexao = "Data Source=WIN-4EF7UMIGTTO;Initial Catalog=ERP;Integrated Security=SSPI";
string sql = @"Update ERP_Cliente Set nome = @nome , endereco = @endereco, bairro = @bairro,cidade = @cidade, estado = @estado, cep = @cep,
telefone = @telefone Where codcliente = @codcliente" ;

SqlConnection con = new SqlConnection(conexao);
SqlCommand comando = new SqlCommand(sql, con);

comando.Parameters.Add(new SqlParameter("@codcliente", entidade["jd_codigo"].ToString()));
comando.Parameters.Add(new SqlParameter("@nome", entidade["name"].ToString()));
comando.Parameters.Add(new SqlParameter("@endereco", entidade["address1_name"].ToString()));
comando.Parameters.Add(new SqlParameter("@bairro", entidade["address1_line1"].ToString()));
comando.Parameters.Add(new SqlParameter("@cidade", entidade["address1_city"].ToString()));
comando.Parameters.Add(new SqlParameter("@estado", entidade["address1_stateorprovince"].ToString()));
comando.Parameters.Add(new SqlParameter("@cep", entidade["address1_postalcode"].ToString()));
comando.Parameters.Add(new SqlParameter("@telefone", entidade["telephone1"].ToString()));

try
{
con.Open();
comando.ExecuteNonQuery();
con.Close();
}
catch (Exception ex)
{
return;
}
}
}
}
}
}
}


Build and take out all the errors that may have appeared.
In the next posting we will register the plugin, and how to debug.








Seja o primeiro a comentar 0 comentários

Postar um comentário

Arquivo do blog

Números de Visitas

  

TOPO