Using Ctrl + S to Save the HTML Webresource Content

By | September 1, 2014

You might be wondering if it would any day be possible to achieve the same behavior of Ctrl + S that we get on CRM pages, on the HTML web resource that we use in CRM.

Well, it is very much possible to do so. There is only one thing you need to take care of while creating the HTML web resource, you need to Prevent the default behavior of Ctrl + S.

How to prevent the default behavior of Ctrl + S on an HTML web resource? Hmm…It is not the difficult question to answer, rather a very simple one.

Below code snippet will help you get the answer to your question:

Code:

$(document).keydown(function (e) {

if ((e.which == ‘115’ || e.which == ’83’) && (e.ctrlKey || e.metaKey)) {

e.preventDefault();

function save() {

//Start Region

//Within this region will be your code to execute

alert(“Ctrl-s pressed”);

parent.Xrm.Page.getAttribute(“name”).setValue(/*The value that you need to set for the field*/);

parent.Xrm.Page.data.entity.save();

//End Region

}

// use setTimeout() to execute

/*The setTimeout is used in order to delay the function, If the function

is fired on the pressing of ctrl + s then it causes the Save As dialog to Pop-Up*/

setTimeout(save, 100);

return false;

}

return true;

});

It is this simple to use Ctrl + S in your favor. This code can be inserted in the <head> tag of the HTML page or else you can put it in the external java script file and refer it in the <head>.

Well, after this, most people think we have saved the data, but the CRM form still shows the previous data, since, it is not refreshed.

Do we have to refresh the form manually in order to reflect the update? Do we have to leave that decision on our end user? The answer to this question is NO!. You can programmatically refresh the page in order to reflect the changes.

So, the question arises, how do we do it? The answer to this question is also very simple. All you have to do is insert the below code after parent.Xrm.Page.data.entity.save(); this statement in the above code.

Code:

Xrm.Utility.openEntityForm(entityname, guid);

It takes two parameters:

entityname -> Name of the current entity.

guid              -> The guid of the current record.

This way you will be able to reload to page.

Note:

  • For this demo we have embedded the webresource on the CRM form. Same features can be achieved on the Pop-Up HTML webresource as well. If you using Pop-Up HTML resource, then you cannot save the parent form programmatically.

  • This code is supported from IE9 onwards, and rest major browsers.

Leave a Reply