从Microsoft Dynamics 365在线调用外部api [英] Call external api from Microsoft Dynamics 365 online

查看:414
本文介绍了从Microsoft Dynamics 365在线调用外部api的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

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

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

这篇关于从Microsoft Dynamics 365在线调用外部api的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆