web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Set CRM Lookup Value (another Entity) using WebAPI

(0) ShareShare
ReportReport
Posted on by

I can create a New Entity but I'm trying to create an Entity with the lookup value to another Entity.  The code below works until I uncomment out the line to set the lookup value.  It always comes back as Bad request.

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 = "dev.company.com/.../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"));
}
}

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Brad Sprigg Profile Picture
    985 on at

    Make sure you get the capitalisation of the navigation property correct, I was scratching my head over a similar issue until I dumped the metadata of an entity out to the console and realised that the navigation property of my custom lookup field wasn't all lower case. I would suggest trying the capitalisation listed in the properties of the field first of all.

  • Inogic Profile Picture
    756 on at

    The issue seems  to be because of the  “col_contactid” name used to set the lookup field. As for Setting lookups  of  the  Custom Entity or setting lookups on Custom Entity you need to specify the <NavigationProperty Name= “”>. You may get it by downloading the OData Metadata from the Developer Resources and search for the attribute name i.e. “col_contactid”. Verify the NavigationProperty Name and the name used in the code. They should match.

    Thanks,

    Sam

  • Community Member Profile Picture
    on at

    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.

    inogic.com/.../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=*");

  • Community Member Profile Picture
    on at

    Thanks, Your blog really helped.  Working on setting up an Online CRM environment so I can post examples on Getting, Updating, Deleting and updating Entities through WebAPI for other people as well.  

  • Aishwarya Profile Picture
    36 on at

    I followed the steps as said 

    "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=*");"

     

    But I am getting an error ""')' or ',' expected at position 8 in (XXX-Guid)"

    Is there any different way to pass the value?

  • Community Member Profile Picture
    on at

    contactid is a managed field, so you wouldn't have your publisher prefix on it. Try "contactid@odata.bind" and see if that works

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
AS-17030037-0 Profile Picture

AS-17030037-0 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans