Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Call Web API service from Plugin

Posted on by 5

Dear members,

At the moment I am having a System.Security.Permission problem (Request Failed message). I want to send a web API service message via a plugin. This must be done via an HttpRequest object because an SSL connection has to be set up. The plugin is executed in a sandbox.
In the HttpRequest object, which is an implementation of a WebClient object, a certificate is also included.

Code snippet:

internal class EngieApiWebClient : WebClient
{
private readonly IOrganizationService _organizationservice;

public ApiWebClient(IOrganizationService service)
{
_organizationservice = service;
}

protected override WebRequest GetWebRequest(Uri address)
{
// Get the request...
HttpWebRequest request = (HttpWebRequest)base.GetWebRequest(address);
// ...and add the certificate

AuthCertificate.GetCertificate(_organizationservice);
request.ClientCertificates.Add(AuthCertificate.Certificate);
// Return the request
return request;
}

}

The call in the plugin is:

using (var wc = new ApiWebClient(CrmOrganisationService))
{
     try
     {
          var data = wc.DownloadString($"{WebApiUrl}{SearchMethod}/{Postalcode}/{Housenumber}");
          if (data.Length > 0) {
                   ConnectionResult connections = JsonConvert.DeserializeObject<ConnectionResult>(data);
            if (connections.IsSuccess)
               {
                  connections.Result.ForEach(c =>
                     {
                         CreateConnectionInCRM(c);
                     }
                  });
           }
        }
        catch (WebException we)
        { }
    }
}

ConnectionResult is a Model Class.

What should i do to solve the problem so the plugin will execute normally without permission exceptions.

Regards,

Hans

*This post is locked for comments

  • RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: Call Web API service from Plugin

    Hi,

    Could you please share you complete code? How are you referencing the certificate, is it stored on your local drive?

  • Hans Heeren Profile Picture
    Hans Heeren 5 on at
    RE: Call Web API service from Plugin

    The security protocol has no effect,

    I do receive the error : System.Security.SecurityException: Request Failed when i want to download data from an API webservice .

    In the WebClient i added a certificate which is accepted by the customer who holds the API webservice, If I execute the plugin in the debugger all is worked fine and as expected. (In isolated mode "Sandbox" with the plugin registration tool)

    The plugin executing is failed in a portal which is build from CRM online.  

  • Suggested answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: Call Web API service from Plugin

    Hi,

    Could you please add Security Protocol in your code  -

    public static OrganizationServiceProxy GetOrganizationServiceProxy()
    {
    ClientCredentials clientCredentials = new ClientCredentials();
     
    clientCredentials.UserName.UserName = "username";
    clientCredentials.UserName.Password = "password";
     
    // Set security protocol to TLS 1.2 for version 9.0 of Customer Engagement Platform
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
     
    return new OrganizationServiceProxy(new Uri("orgname.crm.dynamics.com/.../Organization.svc"),
    null, clientCredentials, null

    }

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans