2017-10-13 119 views
0

我在CRM中拥有全局操作。它有一个输入字符串参数。 这就是呼叫发送请求C#在MS Dynamics CRM中调用全局操作

obj.CallActivity("this is parameter string","MyActionName") 

和我的方法CallActivity看起来像这样

public async Task<bool> CallActivity(string record, string Activity) 
     { 
      try 
      { 

       HttpRequestMessage requestMessage = null; 
       requestMessage = new HttpRequestMessage(HttpMethod.Post, App.ResourceUri + "/api/data/v8.2/" + Activity); //uri to activity 
       requestMessage.Content =new StringContent(record); 
       requestMessage.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json"); 
       //Send the HttpRequest 
       Task<HttpResponseMessage> response = httpClient.SendAsync(requestMessage); 
        response.Wait(); 
       //If the response is Successfully executed then it will return the value true 
       if (response.Result.IsSuccessStatusCode) 
       { 
        return true; 
       } 
       else 
       { 
        return false; 
       } 
      } 
      catch (Exception error) 
      { 

       return false; 
      } 
     } 

当执行请求,我得到的消息:

的StatusCode:400 ,ReasonPhrase:'Bad Request',版本:1.1,内容:System.Net.Http.StreamContent,标题:

我在哪里犯错?

+0

请问您可以添加实际参数值吗?这个错误很可能出现在字符串参数“Activity”的构建中。 – Filburt

+0

obj.CallActivity(“{”id_contact“:”452e368a-1783-e711-8102-70106faa95f1“,”id_car“:”45436d5b-d19d-e711-8101-70106faa5221“}”,“MyActionName”)。输入字符串参数需要像JSON字符串 – user238271

+0

我假设你的“MyActionName”由一个发布者前缀和你的Action模式名称组成,所以这不应该成为问题。但是,您并未设置O-Data版本标题。我已经看到Web API抛弃了这个问题,所以你可能想尝试添加'“OData-MaxVersion”,“4.0”'和“”OData-Version“,”4.0“'头文件。 – Filburt

回答

0

下面是我用来从插件调用我的动作的代码。

发件人regardingcontact是我行动的两个参数,并能正常工作对我来说。

OrganizationRequest req = new OrganizationRequest("crmp_emailbenachrichtigunganeventowner468fcc7975b9e71180e6005056936953"); 

         req["Sender"] = new EntityReference(temp.GetAttributeValue<EntityReference>("ownerid").LogicalName, temp.GetAttributeValue<EntityReference>("ownerid").Id); 

         req["regardingcontact"] = new EntityReference(context.PrimaryEntityName, context.PrimaryEntityId); 
         OrganizationResponse response = service.Execute(req);