从Microsoft Dynamics 365在线调用外部api

时间:2019-03-14 05:14:50

标签: c# .net dynamics-crm dynamics-365

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

会更好

1 个答案:

答案 0 :(得分:1)

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

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