Skip to main content

Notifications

Customer experience | Sales, Customer Insights,...
Suggested answer

How to CRUD contact using Odata API?

Posted on by 10

Hi,

I go with this link for update the entities with API, 

https://docs.microsoft.com/en-us/powerapps/developer/common-data-service/webapi/update-delete-entities-using-web-api

the sample works well, with account changed, 

however , if I apply the same to contacts, like 

change the uri to 

PATCH [Organization URI]/api/data/v9.1/contacts(00000000-0000-0000-0000-000000000001) HTTP/1.1

and content

{ "Firstname": "Updated first name " }

I get error 

 "code""0x0",
        "message""An error occurred while validating input parameters: Microsoft.OData.ODataException: Does not support untyped value in non-open type.\r\n   at System.Web.OData.Formatter.Deserialization.DeserializationHelpers.ApplyProperty(ODataProperty property, IEdmStructuredTypeReference resourceType, Object resource, ODataDeserializerProvider deserializerProvider, ODataDeserializerContext readContext)\r\n   at System.Web.OData.Formatter.Deserialization.ODataResourceDeserializer.ApplyStructuralProperties(Object resource, ODataResourceWrapper resourceWrapper, IEdmStructuredTypeReference structuredType, ODataDeserializerContext readContext)\r\n   at Microsoft.Crm.Extensibility.CrmODataEntityDeserializer.ApplyStructuralProperties(Object resource, ODataResourceWrapper resourceWrapper, IEdmStructuredTypeReference structuredType, ODataDeserializerContext readContext)\r\n   at System.Web.OData.Formatter.Deserialization.ODataResourceDeserializer.ReadResource(ODataResourceWrapper resourceWrapper, IEdmStructuredTypeReference structuredType, ODataDeserializerContext readContext)\r\n   at System.Web.OData.Formatter.ODataMediaTypeFormatter.ReadFromStream(Type type, Stream readStream, HttpContent content, IFormatterLogger formatterLogger)",
        
I then tried to get the first contact, serialized to Json, and replace the content with full contact, detail and I still get the error.
what is the special setting with Contact? and can I create/update using api?
Thanks for any help!
Gordon
  • cloflyMao Profile Picture
    cloflyMao 25,198 on at
    RE: How to CRUD contact using Odata API?

    Hi gordon,

    Please let me know whether you had other problem.

    If you had found any answer helped, please kindly mark as verified, it would be greatly appreciated.

    Regards,

    Clofly

  • Suggested answer
    gordon li Profile Picture
    gordon li 10 on at
    RE: How to CRUD contact using Odata API?

    Thanks Clofly,

    It works by change the Firstname to firstname,

    I am using the postman .

  • cloflyMao Profile Picture
    cloflyMao 25,198 on at
    RE: How to CRUD contact using Odata API?

    Hi Gordon,

    Firstname should be firstname, please pay attention to letter case of fields.

    A full code for update a contact:

    var entity = {
      "firstname": "Mike 1",
      "lastname": "Johnson"
    };
    
    var req = new XMLHttpRequest();
    req.open("PATCH", Xrm.Page.context.getClientUrl()   "/api/data/v9.1/contacts(xxxx)", true);
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    req.onreadystatechange = function() {
        if (this.readyState === 4) {
            req.onreadystatechange = null;
            if (this.status === 204) {
                alert("ok");
            } else {
                Xrm.Utility.alertDialog(this.statusText);
            }
        }
    };
    req.send(JSON.stringify(entity));

    In addition, you could automatically create create/update with Web API by CRM Rest Builder.

    It supports for all CRUD operations.

    https://github.com/jlattimer/CRMRESTBuilder

    Regards,

    Clofly

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans