【问题标题】:How do you call an external web api from CRM Plugin?您如何从 CRM 插件调用外部 Web api?
【发布时间】:2018-11-16 18:29:37
【问题描述】:

我是 CRM 开发的新手。我试图弄清楚如何从我的操作后创建阶段调用外部 api 端点。我怎样才能做到这一点?

protected void ExecutePostOrderCreate(LocalPluginContext localContext)
{
    if (localContext == null)
    {
        throw new ArgumentNullException("localContext");
    }

    IPluginExecutionContext context = localContext.PluginExecutionContext;

    Entity postImageEntity = (context.PostEntityImages != null && context.PostEntityImages.Contains(this.postImageAlias)) ? context.PostEntityImages[this.postImageAlias] : null;

    if (postImageEntity != null)
    {
        ProcessOrderCaseType(postImageEntity, localContext);
    }
}

private void ProcessOrderCaseType(Entity order, LocalPluginContext localContext)
{

    int caseType = GetCaseType(order);

    Boolean caseTypeIsWorkersComp = caseType == (int)MLUtility.CaseTypes.WorkersComp;
    Boolean orderHasEamsCaseMaster = OrderHasCaseMaster(order);

    if (caseTypeIsWorkersComp && !orderHasEamsCaseMaster)
    {
        String caseNumber = GetCaseNumber(order);
        Boolean caseNumberIsValid = !String.IsNullOrEmpty(caseNumber);
        if (caseNumberIsValid)
        {
          **//call api**

        }
    }
}

【问题讨论】:

  • 忘记提及了——这是针对 CRM Dynamics 2013 的

标签: asp.net-web-api plugins dynamics-crm crm dynamics-crm-2013


【解决方案1】:

这就像从其他任何地方调用 api 一样。在注册插件注册工具或服务器中的 gac 之前,唯一的额外步骤是将额外的 dll(如 Newtonsoft.Json)合并到主插件 dll 中。

Code sample

var uri = new Uri("<external api url>"); 
 
                var request = WebRequest.Create(uri); 
                request.Method = WebRequestMethods.Http.Get; 
                request.ContentType = "application/json"; 
                try 
                { 
                    string helloWorldString = ""; 
                    using (var response = request.GetResponse()) 
                    { 
                        using (var reader = new StreamReader(response.GetResponseStream())) 
                        { 
                            string tmp = reader.ReadToEnd(); 
                            helloWorldString = JsonConvert.DeserializeObject<string>(tmp); 
                            Debug.Print("I read this string {0}: ", helloWorldString); 
                        } 
                    } 
 
                    var entity = (Entity) context.InputParameters["Target"]; 
                    if (entity.Contains("description")) 
                        entity.Attributes["description"] = helloWorldString; 
                    else 
                        entity.Attributes.Add("description", helloWorldString); 
 
                    //Update Sales Order Entity 
                    if (context.Depth == 1) 
                        service.Update(entity); 
 
                } 
                catch (Exception ex) 
                { 
                    throw; 
                }

【讨论】:

  • 谢谢阿伦。我能够调用我的网络服务并返回响应。但是,有一个异常。问题是我正在使用 CRM 插件注册工具。我创建了一个配置文件,在调试时,该工具在我身上崩溃了。但是,如果我使用跟踪服务并添加一些“面包屑”,一切正常。不知道为什么会这样。有什么想法吗?
  • @JeffreyJuarez 它应该可以工作,不确定。您可以就该主题发布不同的问题。如果此答案解决了您的问题,请点赞/标记。
猜你喜欢
  • 1970-01-01
  • 2014-02-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-30
  • 1970-01-01
  • 2016-05-31
  • 2017-05-17
相关资源
最近更新 更多