Using Web API Actions in CRM 2016

In our last Web API article we discussed about Web API functions, if you have not checked it yet, click here to know about web api functions. Today we are going to discuss about Web API actions. Similarly to function actions are also reusable piece of code and can bound or unbound. There are list of pre define actions, which can be referred from here, but we can also create our own custom action and call them as well. If you are new to actions, please refer our older article to learn about creating actions.

We are going to discuss about AddMemberList action which is a bound action. Bound action or functions are called by using full name of the action or function including Microsoft.Dynamics.CRM namespace, so we need to use it like following:
Microsoft.Dynamics.CRM.AddMemberList

AddMemberList action takes one following parameter:
addmemberaction

Actually for every web api action there is a corresponding organization request, for example AddMemberList has corresponding AddMemberListRequest. If you will check this request you will see it needs three following required properties:
addmemberactionrequest

And this request can be implemented using following server side code:

 AddMemberListRequest request = new AddMemberListRequest()
                  { 
                      ListId=new Guid ("<<GUID of the target list>>"),
                      EntityId = new Guid("<<GUID of the member that we want to add>>")
                   };
                  organizationService.Execute(request);

As you see we need to set basically two properties, listid where we want to add member and the entity id, which we want to add. Ok so back to web api, to implement this action using web api, we need to pass these two properties only. We can use following POST request to implement it:

function AddMemberToList() {
    var serverURL = Xrm.Page.context.getClientUrl();
    var data = {
        "EntityId": "<<GUID of the member that we want to add>>"
    };

    var req = new XMLHttpRequest();
    req.open("POST", serverURL + "/api/data/v8.0/lists(<<GUID of the target list>>)/Microsoft.Dynamics.CRM.AddMemberList", true);
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");
    req.onreadystatechange = function() {
        if (this.readyState == 4 /* complete */ ) {
            req.onreadystatechange = null;
            if (this.status == 200) {
                alert("Member added to Marketing list");
            } else {
                var error = JSON.parse(this.response).error;
                alert(error.message);
            }
        }
    };
    req.send(JSON.stringify(data));
}

Now we can create java script web resource and use above code. We can call AddMemeberToList method on some event. Make sure you are passing member id based on the list member selected while creating marketing list, for example if you have created marketing list for lead, so you need to pass lead id:
marketinglist

In next article we will discuss how we can call our custom action using web api so stay tuned !!

2 thoughts on “Using Web API Actions in CRM 2016

  1. Pingback: Calling Custom Actions using Web API | HIMBAP

  2. Pingback: Executing workflow from command button using Web API | HIMBAP

Leave a Reply

Your email address will not be published. Required fields are marked *