7

In a Joomla application I am getting a user info as follows and then I need to save the user info as a contact in a Dynamics 365 database through their REST API.

                    $user = JFactory::getUser();
                    $username = $user->username;
                    $name = $user->name;

I have looked up Dynamics documents around Web API and REST API like this and this, but none of them provide useful info how I can call the API to add a new contact. Currently, I am connecting to Dynamics 365 web application via this url: http://example.com:8088/mysite/api/data/v8.2. The linked post also talks about REST API, but only querying. I'm looking for a way to post data to Dynamics CRM using REST API.

Mehdi Haghgoo
  • 3,144
  • 7
  • 46
  • 91

1 Answers1

6

The payload to create Contact using crm webapi will look like this: Read more

POST [Organization URI]/api/data/v8.2/contacts HTTP/1.1
Content-Type: application/json; charset=utf-8
OData-MaxVersion: 4.0
OData-Version: 4.0
Accept: application/json

{
    "firstname": "Arun",
    "lastname": "Vinoth"
}

Sorry am not from PHP background, but this link may help you.

Update:
I browsed little bit. Found the below code sample from SO answer. Update the [Organization URI] with CRM URL, for ex. https://testorg.crm.dynamics.com

$url = '[Organization URI]/api/data/v8.2/contacts';
$data = array('firstname' => 'Arun', 'lastname' => 'Vinoth');

// use key 'http' even if you send the request to https://...
$options = array(
    'http' => array(
        'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
        'method'  => 'POST',
        'content' => http_build_query($data),
    ),
);
$context  = stream_context_create($options);
$result = file_get_contents($url, false, $context);

var_dump($result);
  • from where can I get [Organization URI] . I have account https://portal.azure.com/ and all stuff. – Adeel Shekhani Jul 09 '20 at 10:35
  • @AdeelShekhani that’s your crm Org url.. for ex. “https://devcrm.crm.dynamics.com” – Arun Vinoth-Precog Tech - MVP Jul 09 '20 at 14:14
  • Thanks. I found that. Can you please tell me how do I get my hands to fetch,update,insert contacts, users etc .. Docs contains that everywhere /api/data/v8.0/accounts etc. but I couldn't find how to authorize. Right now If I try to fetch contacts it returns 401 complaining about WWW-Authenticate – Adeel Shekhani Jul 09 '20 at 14:18
  • @AdeelShekhani it’s more or less same, just switch the schema names in web api endpoints and use the right attributes schema names. Try crm rest builder for composing queries. For authentication, you have to use azure app I’d registration and token methods. Start from postman or something for initial setup and testing – Arun Vinoth-Precog Tech - MVP Jul 09 '20 at 14:20
  • Is there any url where I can find any guidance. I am using postman to authorize and https://login.microsoftonline.com/{tenant}/authorize with client_id in the POST body, but response I get is 'Sign in to your account" . also "the request body must contain the following parameter: \u0026#39;client_id\u0026#39 – Adeel Shekhani Jul 09 '20 at 14:37
  • 1
    @AdeelShekhani google is best, check this https://rajeevpentyala.com/2019/02/13/step-by-step-postman-tool-to-test-dynamics-365-online-web-api/ – Arun Vinoth-Precog Tech - MVP Jul 09 '20 at 15:00