web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Answered

How to connect Plugin to External Rest API

(0) ShareShare
ReportReport
Posted on by 20

Hi All,

I have one requirement where i need to call a rest API from my plugin without using ILmerge for Online Dynamics 365.

Please provide me any code sample so I can start working .

I have the same question (0)
  • Suggested answer
    Wahaj Rashid Profile Picture
    11,321 on at

    Hi,

    Here is a great tutorial by Nishant:

    nishantrana.me/.../

    Let us know if you need further help.

    Best,

    Wahaj

    (if it helps, please mark it as verified)

  • ShubhamJain7 Profile Picture
    20 on at

    Thanks for the link .

    I have tried the code in this way for the "Get" method , but getting error at httprequest call.

    code:

    public class pluginwebclient :IPlugin
    {
    public void Execute(IServiceProvider serviceProvider)
    {
    try
    {
    // Obtain the execution context from the service provider.
    IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
    IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
    IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
    // The InputParameters collection contains all the data passed in the message request.
    if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
    {
    // Obtain the target entity from the input parameters.
    Entity entity = (Entity)context.InputParameters["Target"];
    if (entity.LogicalName != "contact")
    return;

    string serviceUrl = string.Format("">dummy.restapiexample.com/.../1");

    WebRequest request = WebRequest.Create(serviceUrl);
    request.Method = "GET";
    request.ContentType = "Application/Json";
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    string result = string.Empty;
    using (Stream stream = response.GetResponseStream())
    {
    StreamReader sr = new StreamReader(stream);
    result = sr.ReadToEnd();
    sr.Close();
    }


    entity.Attributes.Add("jobtitle", result);
    }
    }
    catch (Exception ex)
    {
    throw new InvalidPluginExecutionException(ex.Message);
    }

  • cloflyMao Profile Picture
    25,210 on at

    Hi ShubhamJain7,

    You need to check whether authentication is required to call the API.

    e.g: add token to your request header or whether the origin URL need to be add as value of HTTP/Access-Control-Allow-Origin header at Web API service provider side.

    If its the second situation, try to add your organization URL as allowed origin in configuration page of Web API service provider.

    Regards,

    Clofly

  • Suggested answer
    Wahaj Rashid Profile Picture
    11,321 on at

    HI,

    It would be easier to diagnose using Postman.

    Test the API and request using Postman.

    Furthermore, make sure you are using correct request headers and authentication method.

    Best,

    Wahaj

  • ShubhamJain7 Profile Picture
    20 on at

    I tried from Postman and its working fine but getting this error while trying from plugin--

    Request for the permission of type 'System.Security.Permissions.SecurityPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.

  • Wahaj Rashid Profile Picture
    11,321 on at

    Hi,

    I guess you have registered your plugin in sandbox mode.

    Can you try registering the plugin without Isolation mode (select none):

    pastedimage1608722354806v1.png

    Best,

    Wahaj

  • ShubhamJain7 Profile Picture
    20 on at

    yes its online Dynamics 365--so i can't select none

  • cloflyMao Profile Picture
    25,210 on at

    Hi ShubhamJain7,

    Is the web API endpoint running on port 80 or 443?

    https://community.dynamics.com/crm/f/microsoft-dynamics-crm-forum/268730/ws-call-dynamics-v9-online-permissions-plugin/761186

    Regards,

    Clofly

  • ShubhamJain7 Profile Picture
    20 on at

    Hi Mao,

    For now I am just working on POC so using some online dummy API's  from dummy.restapiexample.com/ ..do you have any online free API link which uses port 80 or 443 to test my plugin..

  • Verified answer
    cloflyMao Profile Picture
    25,210 on at

    Hi ShubhamJain7,

    You could try calling this API, it is open without authentication requirement.

    https://api.ip.sb/ip

    It will return the IP of our machine.

    I tested it with your code, it will get IP of  Dynamics server and populate job title field of contact with the value.

    Regards,

    Clofly

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 170 Super User 2025 Season 2

#2
#ManoVerse Profile Picture

#ManoVerse 61

#3
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 52 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans