RSS

Calling external Web Service from a CRM 2011 Plug-in

14 Jun

Introduction

This blog explains how to call external Web Service from a CRM 2011 Plug-in.

Background

Sometimes we need to call a Web Service from a Plug-in. We had a similar requirement, but while trying to access the service, we got the following error.

“Could not find default endpoint element that references contract ‘ServiceReference.Service′ in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element.”

Problem

The reason for this is because the configuration information for the Web Service from the client side is missing.

Using the Code

So, now in case of our plugin, we need to define the binding and endpoint information programmatically. Something like this…

using System;
using System.ServiceModel;
using Microsoft.Xrm.Sdk;
using TestPluginConnectWebService.TestService;

namespace TestPluginConnectWebService
{
    public class TestConnectWebService : 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));

            //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:///ReturnRecords/GetRecords.asmx");

            // Call to the Web Service using the Binding and End Point Address.
            GetGrantRecordsSoapClient myClient = new GetGrantRecordsSoapClient(myBinding, endPointAddress);

            var ab = myClient.GetGrants();

            if (ab != null)
            {
                 throw new InvalidPluginExecutionException("Success");
            }
            else
            {
                 throw new InvalidPluginExecutionException("Failure");
            }
        }
    }
}

This way we would be able to access our Web Service inside Plug-in.

Note

The following are some of the points that needs special attention.

Sandboxed Plug-ins can access network through the HTTP and HTTPS protocols. This capability provides support for accessing popular Web resources like Social Sites, News Feeds, Web services, and more.
The following Web access restrictions apply to this Sandbox capability.

  • Only the HTTP and HTTPS protocols are allowed.
  • Access to localhost (loopback) is not permitted.
  • IP addresses cannot be used. You must use a named Web address that requires DNS name resolution.

Reference – [MSDN] Plug-in Isolation, Trusts, and Statistics

By Dibyasingh Tripathy
Senior Software Engineer @Team DynamicsCRM.
Mindfire Solutions

Advertisements
 

Tags: , , , , , , , , , , , , , , , , , , , , , ,

2 responses to “Calling external Web Service from a CRM 2011 Plug-in

  1. Olena Grischenko

    February 17, 2014 at 10:55 am

    This is great! 🙂 you saved my day

     
  2. DynamicsCRM@MindfireSolutions

    February 20, 2014 at 9:22 am

    Thanks Olena 🙂

     

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: