从 Microsoft Dynamics 365 在线调用外部 api

Call external api from Microsoft Dynamics 365 online

有谁知道如何从 Dynamics 365 在线调用外部 Web api。当机会状态更改为已批准时,我需要发送 post http 请求。如果使用 .net

会更好

这是从 CRM Online 调用外部 API 的示例代码。我有一个更改帐户的触发器(即帐户上的任何文件更改我的插件都会触发)。这反过来会调用 public API 并读取它的内容和响应。有大量可用的示例。

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

namespace WhosebugExamples
{

    /// <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 WhosebugEx1 : PluginBase
    {
    /// <summary>
    /// Initializes a new instance of the <see cref="WhosebugEx1"/> 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 WhosebugEx1(string unsecure, string secure)
        : base(typeof(WhosebugEx1))
    {

        // 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);*/
    }
}
}