Search code examples
c#dynamics-crmcrmdynamics-crm-2016dynamics-crm-webapi

how to execute crm book action via web api?


CRM exposes actions, and it allows you to execute them via Web API.

For example the following is the WinOpportunity action schema and API:

<Action Name="WinOpportunity">
  <Parameter Name="OpportunityClose" Type="mscrm.opportunityclose" Nullable="false" />
  <Parameter Name="Status" Type="Edm.Int32" Nullable="false" />
</Action>

And to execute this question, you would POST the following:

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

{
 "Status": 3,
 "OpportunityClose": {
  "subject": "Won Opportunity",
  "[email protected]": "[Organization URI]/api/data/v8.2/opportunities(b3828ac8-917a-e511-80d2-00155d2a68d2)"
 }
}

Is there a way to execute the BookRequest Action?

In examining the CSDL schema, I found that this action is defined as:

<Action Name="Book">
<Parameter Name="Target" Type="mscrm.crmbaseentity" Nullable="false"/>
<Parameter Name="ReturnNotifications" Type="Edm.Boolean"/>
<ReturnType Type="mscrm.BookResponse" Nullable="false"/>
</Action>

What will the request look like for this Book action?


Solution

  • Referring MSDN, the BookRequest message expects Appointment as Target. Book action as well.

    // Create the ActivityParty instance.
    ActivityParty party = new ActivityParty
    {
        PartyId = new EntityReference(SystemUser.EntityLogicalName, userResponse.UserId)
    };
    
    // Create the appointment instance.
    Appointment appointment = new Appointment
    {
        Subject = "Test Appointment",
        Description = "Test Appointment created using the BookRequest Message.",
        ScheduledStart = DateTime.Now.AddHours(1),
        ScheduledEnd = DateTime.Now.AddHours(2),
        Location = "Office",
        RequiredAttendees = new ActivityParty[] { party },
        Organizer = new ActivityParty[] { party }                        
    };                    
    
    // Use the Book request message.
    BookRequest book = new BookRequest
    {
        Target = appointment
    };
    

    Referring MSDN, webapi request may look like this: (am using existing appointment record, still getting 400 Bad request)

    POST [Organization URI]/api/data/v8.2/Book HTTP/1.1
    Accept: application/json
    Content-Type: application/json; charset=utf-8
    OData-MaxVersion: 4.0
    OData-Version: 4.0
    
    {
     "Target": {
      "activityid": "59ae8258-4878-e511-80d4-00155d2a68d1",
      "@odata.type": "Microsoft.Dynamics.CRM.appointment"
     }
    }