How to call AddUserToRecordTeam action using Web API

Welcome to my old Blogspot blog! You can read this post on our new website: Read this post on our new website 

Nothing special, but I'm posting here a simple JS function that calls the AddUserToRecordTeam action using web API. It took some time for me, especially in setting/fixing the parameters. Posting here reference.

    /*
    entityId: Guid of entity record
    entityLogicalName: Logical name of the entity, like lead/contact etc
    userId: Guid of the systemuser 
    templateId: team template id
    */
    function createAccessTeam(entityId,entityLogicalName,userId,templateId)
    {
        var parameters = {
            Record: { [entityLogicalName+"id"]: entityId, "@odata.type": "Microsoft.Dynamics.CRM."+entityLogicalName },
            TeamTemplate:{ "teamtemplateid": templateId, "@odata.type": "Microsoft.Dynamics.CRM.teamtemplate" }
        };
        var req = new XMLHttpRequest();
        req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/systemusers("+userId+")/Microsoft.Dynamics.CRM.AddUserToRecordTeam", 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 === 200) {
                    var results = JSON.parse(this.response);
                } else {
                    Xrm.Utility.alertDialog(this.statusText);
                }
            }
        };
        req.send(JSON.stringify(parameters));
    }
Below is an example to call the above function:
var entityId="db4633b7-56d1-e811-a979-000d3af3e0db";
var entityLogicalName="lead",
var userId ="db4633b7-56d1-e811-a979-000d3af3e0db"
var templateId="45CED4C4-A971-EB11-A812-000D3AF39D99"
createAccessTeam(entityId,entityLogicalName,userId,templateId);

No comments:

Post a Comment