Creating Task entity record through rest in MS CRM 2011

In my Adding Convert Email To Task in Email Ribbon MS CRM 2011  post I have explained how to create ribbon button to copy email into task, in this post I am providing code to create task entity record through rest from email.

function ConverToTask() {

var context = Xrm.Page.context;

var serverUrl = context.getServerUrl();

var ODataPath = serverUrl + “/XRMServices/2011/OrganizationData.svc”;

var task = new Object();


task.Subject = Xrm.Page.getAttribute(“subject”).getValue();



var Regarding = Xrm.Page.getAttribute(“regardingobjectid”).getValue();

task.RegardingObjectId = { Id: Regarding[0].id, LogicalName: Regarding[0].typename, Name: Regarding[0].name };


var Task = this.parent.JSON.stringify(task);var CreateRequest = new XMLHttpRequest();“POST”, ODataPath + “/TaskSet”, false);

CreateRequest.setRequestHeader(“Accept”, “application/json”);

CreateRequest.setRequestHeader(“Content-Type”, “application/json; charset=utf-8”);


if (CreateRequest.readyState == 4 /* complete */) {

if (CreateRequest.status == 201) {

var retrievedRecords = this.parent.JSON.parse(CreateRequest.responseText).d;

var ID = retrievedRecords.ActivityId; +“/userdefined/edit.aspx?etc=4212&id=”+ ID); //configure URL and open new window




Enjoy !!!

Leave a comment

Filed under MS CRM 2011

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s