Set CRM Lookup Values with WebAPI

牧云@^-^@ 提交于 2019-12-22 10:44:38

问题


Who had done CRM Web API calls to update CRM entities with Lookup values from another Entity.

I'm trying to set a Lookup Value to another Entity within CRM using WebAPI, CRM 2016. It works if I disable the Lookup value but once I enable the Lookup value, I receive Bad Request.

Below is my code in LinqPad so it does work.

void Main()
{   
using(var webClient = new WebClient()){
    webClient.Credentials = new NetworkCredential("Username", "Password", "Domain");
    webClient.Headers.Add("OData-MaxVersion", "4.0");
    webClient.Headers.Add("OData-Version", "4.0");
    webClient.Headers.Add("accept", "application/json");
    webClient.Headers.Add("Content-Type","application/json");
    webClient.Headers.Add("Prefer", "odata.include-annotations=*");         

    webClient.BaseAddress = "http://dev.company.com/DEV2016/api/data/v8.0/";

    var JO = new JObject();
    JO.Add("col_name","My Name");
    //JO.Add("col_contactid@odata.bind","/contacts(7266f26b-7105-e611-811e-005056b61789)");
    var dataString = JO.ToString();

    var responseString = webClient.UploadString("col_advisors", "POST", dataString);

    Console.WriteLine(webClient.ResponseHeaders.Get("OData-EntityId"));
}
}

回答1:


Case matters with the WebAPI. Make sure your col_contactid is the schema name, not the logical name. For example, the logical name of your attribute is col_contactid (logical names are always lowercase), but schema names often times have upper case letters. Yours might be col_ContactId for example, in which case you would want to use col_ContactId@odata.bind.

The easiest way to find the schema name of your attribute is by going to CRM -> Settings -> Solutions -> your solution -> Entites (on the left) -> Advisors -> Fields. In that grid you'll see a column for schema name.




回答2:


I got it to work. The fields really have to be unique as it is case sensitive. Comments here and also this blog, really helped.

http://inogic.com/blog/2016/02/set-values-of-all-data-types-using-web-api-in-dynamics-crm/

Step 1 : Goto Cutomization  Developer Resource.

Step 2 : Click to “Download Odata Metadata” link and Download the same.

Step 3 : Once Download, open it and find out name of lookup attribute ( i.e. new_qualifiedleadid) and check its casing.

Step 4 : Verify it with the value which you are setting in the code it should be same.

While my column was col_contactid, CRM renames the Navigational Column to be what was above col_ContactId.

I also used Postman(google chrome) plugin and added the following Header to my Post.

webClient.Headers.Add("Prefer", "odata.include-annotations=*");


来源:https://stackoverflow.com/questions/36700796/set-crm-lookup-values-with-webapi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!