Here is a quick sample code how to send a value from CRM plugin to an external web services:
Web service code :
Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.Data
Imports System.Data.Sql
' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
' <System.Web.Script.Services.ScriptService()> _
<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Public Class WebService
Inherits System.Web.Services.WebService
<WebMethod()> _
Public Function IsContact(ByVal ID As String) As String
Dim ConnectionString As String = "Data Source=CRM\SQL;Initial Catalog=Demo_DB;User ID=sa;Password=P#$$S2016"
Dim SqlConn As New SqlClient.SqlConnection
Dim SQlCmd As New SqlClient.SqlCommand
Dim SQLDr As SqlClient.SqlDataReader
SqlConn.ConnectionString = ConnectionString
SQlCmd.Connection = SqlConn
SQlCmd.CommandText = "SELECT [fullname] FROM [dbo].[TContact] WHERE [ContactId] ='" & ID & "'"
Try
SqlConn.Open()
SQLDr = SQlCmd.ExecuteReader
If SQLDr.Read() Then
Return SQLDr.Item(1).ToString
Else
Return False
End If
SqlConn.Close()
Catch ex As Exception
SqlConn.Close()
Return ""
End Try
End Function
End Class
Plugin Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Client;
using Microsoft.Crm.Sdk;
using ServiceReference1;
namespace Test_Call_WS
{
public class Test_Call_WS : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
//Context = Info passed to the plugin at runtime
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
Entity entity = (Entity)context.InputParameters["Target"];
if (entity.LogicalName != "contact")
{
return;
}
//Service = access to data for modification
IOrganizationService service = factory.CreateOrganizationService(context.UserId);
// Adding Basic Http Binding and its properties.
BasicHttpBinding myBinding = new BasicHttpBinding();
myBinding.Name = "BasicHttpBinding_Service";
myBinding.Security.Mode = BasicHttpSecurityMode.None;
myBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
myBinding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.None;
myBinding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName;
// Endpoint Address defining the asmx Service to be called.
EndpointAddress endPointAddress = new EndpointAddress(@"http://CRM:88/WebService.asmx");
// Call to the Web Service using the Binding and End Point Address.
ServiceReference1.WebServiceSoapClient myClient = new ServiceReference1.WebServiceSoapClient(myBinding, endPointAddress);
string ab = myClient.IsContact(entity.Id.ToString());
if (ab != "")
{
throw new InvalidPluginExecutionException(ab);
}
else
{
throw new InvalidPluginExecutionException("Failure");
}
}
}
}
}