2017-10-13 98 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);