Call external api from Microsoft Dynamics 365 online

◇◆丶佛笑我妖孽 提交于 2020-02-06 18:50:39

问题


Does anybody know how to call external web api from Dynamics 365 online. I need to send a post http request when opportunity status is changed to approved. It would be better if using .net


回答1:


Here is sample code for calling External API from CRM Online. I have a trigger on change of Account (i.e any filed on account is changed my plugin will fire). This in turn will call public API and read it's content and response. There are ample amount of Examples available.

using System;
using System.IO;
using System.Net;
using System.ServiceModel;
using System.Text;
using Microsoft.Xrm.Sdk;

namespace StackOverFlowExamples
{

    /// <summary>
    /// PluginEntryPoint plug-in.
    /// This is a generic entry point for a plug-in class. Use the Plug-in Registration tool found in the CRM SDK to register this class, import the assembly into CRM, and then create step associations.
    /// A given plug-in can have any number of steps associated with it. 
    /// </summary>    
    public class StackOverflowEx1 : PluginBase
    {
    /// <summary>
    /// Initializes a new instance of the <see cref="StackOverflowEx1"/> class.
    /// </summary>
    /// <param name="unsecure">Contains public (unsecured) configuration information.</param>
    /// <param name="secure">Contains non-public (secured) configuration information. 
    /// When using Microsoft Dynamics CRM for Outlook with Offline Access, 
    /// the secure string is not passed to a plug-in that executes while the client is offline.</param>
    public StackOverflowEx1(string unsecure, string secure)
        : base(typeof(StackOverflowEx1))
    {

        // TODO: Implement your custom configuration handling.
    }


    /// <summary>
    /// Main entry point for he business logic that the plug-in is to execute.
    /// </summary>
    /// <param name="localContext">The <see cref="LocalPluginContext"/> which contains the
    /// <see cref="IPluginExecutionContext"/>,
    /// <see cref="IOrganizationService"/>
    /// and <see cref="ITracingService"/>
    /// </param>
    /// <remarks>
    /// For improved performance, Microsoft Dynamics CRM caches plug-in instances.
    /// The plug-in's Execute method should be written to be stateless as the constructor
    /// is not called for every invocation of the plug-in. Also, multiple system threads
    /// could execute the plug-in at the same time. All per invocation state information
    /// is stored in the context. This means that you should not use global variables in plug-ins.
    /// </remarks>
    protected override void ExecuteCrmPlugin(LocalPluginContext localContext)
    {
        if (localContext == null)
        {
            throw new ArgumentNullException("localContext");
        }

        // TODO: Implement your custom plug-in business logic.

        IPluginExecutionContext context = localContext.PluginExecutionContext;
        IOrganizationService service = localContext.OrganizationService;
        ITracingService tracingService = localContext.TracingService;

        callRestAPI1();
        callRestAPI2();
    }



    private void callRestAPI2()
    {
        var request = (HttpWebRequest)WebRequest.Create("https://api.predic8.de/shop/products/");
        request.Method = "GET";
        request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36";
        request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
        var response = (HttpWebResponse)request.GetResponse();
        string content = string.Empty;
        using (var stream = response.GetResponseStream())
        {
            using (var sr = new StreamReader(stream))
            {
                content = sr.ReadToEnd();
            }
        }
        throw  new Exception($"api data is {content}");
    }

    private void callRestAPI1()
    {
        var request = (HttpWebRequest)WebRequest.Create("https://api.github.com/repos/restsharp/restsharp/releases");
        request.Method = "GET";
        request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36";
        request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
        var response = (HttpWebResponse)request.GetResponse();
        string content = string.Empty;
        using (var stream = response.GetResponseStream())
        {
            using (var sr = new StreamReader(stream))
            {
                content = sr.ReadToEnd();
            }
        }
        /*var releases = JArray.Parse(content);*/
    }
}
}


来源:https://stackoverflow.com/questions/55155379/call-external-api-from-microsoft-dynamics-365-online

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!