0

I want to integrate Pipedrive with Dynamics CRM. I am calling webhook whenever deal is updated in Pipedrive. I am getting data in JObject form. I want to extract two fields from that. One is status and other is title.How can I do that?

Code to call Webhook:

 [RoutePrefix("api/webhook")]
public class WebhookController : ApiController
{
    [HttpPost]
    [Route("")]
    public void Post(JObject message)
    {
        Console.WriteLine($"Received webhook: {message}");            
    }
}

Json which I am getting :

{
  "v":1,
  "matches_filters":{
    "current":[

    ]
  },
  "meta":{
    "v":1,
    "action":"updated",
    "object":"deal",
    "id":1,
    "company_id":2278508,
    "user_id":3371496,
    "host":"agniket.pipedrive.com",
    "timestamp":1512620040,
    "timestamp_micro":1512620040475200,
    "permitted_user_ids":[
      3371496
    ],
    "trans_pending":false,
    "is_bulk_update":false,
    "pipedrive_service_name":false,
    "matches_filters":{
      "current":[

      ]
    },
    "webhook_id":"31814"
  },
  "current":{
    "id":1,
    "public_id":null,
    "creator_user_id":3371496,
    "user_id":3371496,
    "person_id":1,
    "org_id":null,
    "stage_id":1,
    "title":"vaishali deal",
    "value":0,
    "currency":"INR",
    "add_time":"2017-12-04 04:44:15",
    "update_time":"2017-12-07 04:14:00",
    "stage_change_time":null,
    "active":false,
    "deleted":false,
    "status":"won",
    "probability":null,
    "next_activity_date":null,
    "next_activity_time":null,
    "next_activity_id":null,
    "last_activity_id":null,
    "last_activity_date":null,
    "lost_reason":null,
    "visible_to":"3",
    "close_time":"2017-12-07 04:14:00",
    "pipeline_id":1,
    "won_time":"2017-12-07 04:14:00",
    "first_won_time":"2017-12-07 04:14:00",
    "lost_time":null,
    "products_count":0,
    "files_count":0,
    "notes_count":0,
    "followers_count":1,
    "email_messages_count":0,
    "activities_count":0,
    "done_activities_count":0,
    "undone_activities_count":0,
    "reference_activities_count":0,
    "participants_count":1,
    "expected_close_date":null,
    "last_incoming_mail_time":null,
    "last_outgoing_mail_time":null,
    "stage_order_nr":1,
    "person_name":"vaishali",
    "org_name":null,
    "next_activity_subject":null,
    "next_activity_type":null,
    "next_activity_duration":null,
    "next_activity_note":null,
    "formatted_value":"₹0",
    "rotten_time":null,
    "weighted_value":0,
    "formatted_weighted_value":"₹0",
    "owner_name":"riya dalvi",
    "cc_email":"agniket+deal1@pipedrivemail.com",
    "org_hidden":false,
    "person_hidden":false
  },
  "previous":{
    "id":1,
    "public_id":null,
    "creator_user_id":3371496,
    "user_id":3371496,
    "person_id":1,
    "org_id":null,
    "stage_id":1,
    "title":"vaishali deal",
    "value":0,
    "currency":"INR",
    "add_time":"2017-12-04 04:44:15",
    "update_time":"2017-12-04 06:33:56",
    "stage_change_time":null,
    "active":true,
    "deleted":false,
    "status":"open",
    "probability":null,
    "next_activity_date":null,
    "next_activity_time":null,
    "next_activity_id":null,
    "last_activity_id":null,
    "last_activity_date":null,
    "lost_reason":null,
    "visible_to":"3",
    "close_time":null,
    "pipeline_id":1,
    "won_time":null,
    "first_won_time":null,
    "lost_time":null,
    "products_count":0,
    "files_count":0,
    "notes_count":0,
    "followers_count":1,
    "email_messages_count":0,
    "activities_count":0,
    "done_activities_count":0,
    "undone_activities_count":0,
    "reference_activities_count":0,
    "participants_count":1,
    "expected_close_date":null,
    "last_incoming_mail_time":null,
    "last_outgoing_mail_time":null,
    "stage_order_nr":1,
    "person_name":"vaishali",
    "org_name":null,
    "next_activity_subject":null,
    "next_activity_type":null,
    "next_activity_duration":null,
    "next_activity_note":null,
    "formatted_value":"₹0",
    "rotten_time":null,
    "weighted_value":0,
    "formatted_weighted_value":"₹0",
    "owner_name":"riya dalvi",
    "cc_email":"agniket+deal1@pipedrivemail.com",
    "org_hidden":false,
    "person_hidden":false
  },
  "event":"updated.deal",
  "retry":0
}
AKX
  • 152,115
  • 15
  • 115
  • 172
riya dalvi
  • 33
  • 2
  • 10

3 Answers3

1

If it's a regular HTTP call you can just use built-in JSON model binder. Try to change your JObject to a class which represents your message, like: (Structure updated to provided JSON representation)

public class Message 
{
    public Current Current { get; set; }
}   

public class Current 
{
    public string Status { get; set; }
    public string Title { get; set; }
}

and use it in your WebHook:

[RoutePrefix("api/webhook")]
public class WebhookController : ApiController
{
    [HttpPost]
    [Route("")]
    public void Post(Message message)
    {
        Console.WriteLine($"Received webhook: {message.Current.Title} {message.Current.Status}");            
    }
}
krlm
  • 817
  • 7
  • 18
  • @BrianRogers You're right - but when I was writing my answer there were no JSON representation in OPs question. – krlm Dec 09 '17 at 21:28
  • thanks I got values from this and I also tried that converting into dynamic object I got values using that. – riya dalvi Dec 11 '17 at 05:23
0

You can try like this;

[HttpPost]
[Route("")]
public void Post(JObject message)
{
    var dynamicMessage = (dynamic)message;
    var status = dynamicMessage.status;
    var title = dynamicMessage.title;            
}
lucky
  • 12,734
  • 4
  • 24
  • 46
  • I tried this but I am getting null values for both fields. – riya dalvi Dec 09 '17 at 13:58
  • I reformatted the message to make it easier to read. I think `dynamicMessage.current.status` and `dynamicMessage.current.title` might work. – AKX Dec 09 '17 at 14:37
  • I got values using this. but when i try to pass the values to other method I am getting "Object reference not set to an instance of an object.", – riya dalvi Dec 11 '17 at 05:24
  • How are you doing it ? Would you share that method and error details. – lucky Dec 11 '17 at 05:33
0

I would use SelectToken. You can specify a JSONPath to the target value. Assuming you want the current status and title you would do:

string status = (string)message.SelectToken("current.status");
string title = (string)message.SelectToken("current.title");

If you want the previous status and title, use previous.status and previous.title instead.

Brian Rogers
  • 125,747
  • 31
  • 299
  • 300
  • I am getting null values. – riya dalvi Dec 10 '17 at 09:10
  • It works fine for me in a [console app](https://dotnetfiddle.net/KK9yxS). I also tried it in a brand new Web API 2 project with your controller code and it worked there as well. Verify that your `JObject` has data in it and that the JSON has the same structure as what is posted in your question. If the JSON is different, then you will need to adjust the JSONPaths accordingly. – Brian Rogers Dec 10 '17 at 18:10