Позвоните по внешнему API из Microsoft Dynamics 365 онлайн - PullRequest
0 голосов
/ 14 марта 2019

Кто-нибудь знает, как вызвать внешний веб-API из Dynamics 365 онлайн.Мне нужно отправить HTTP-запрос по почте, когда статус возможности будет изменен на одобренный.Было бы лучше, если использовать .net

1 Ответ

1 голос
/ 14 марта 2019

Вот пример кода для вызова внешнего API из CRM Online.У меня есть триггер на изменение учетной записи (т. Е. Любая поданная на учетную запись запись меняется, мой плагин будет срабатывать).Это в свою очередь вызовет публичный 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);*/
    }
}
}
...