Wednesday 3 September 2014

CRM Dynamics 2013: Use Ctrl+S or click the CRM ‘Save’ icon to save your custom web resource´s entities

So, you have managed to create a custom html-javascript web page which you have inserted in your CRM as described here, right? Also, your page does some database calls and updates and you would like to use the save icon, already built in CRM to submit your changes. If this is the case, read along!

This can be quite tricky to achieve as your webresource is lies within an iframe, which also lies within another iframe because this is how the CRM bilds pages. Also, I have never found a way to use Xrm.Page context here, as it is impossible to pre-define the internal CRM – name that dynamics uses to identify the content for the iframe (maybe someone can help here?)

Therefore I have just used the following code snippet to gain access to the html element and to bind a click event to it:

1 // bind the save (TrySaveAll) function to the save icon down-right
2 if (window.parent.document.getElementById('savefooter_statuscontrol'))
3 window.parent.document.getElementById('savefooter_statuscontrol').addEventListener('click', function (e) {
4 e.preventDefault();
5 TrySaveAll(); // my "save" method
6 });


This is naturally a “hack” and is highly NOT recommended, it works for now, but basically one can never tell if suddenly Microsoft updates something and it just stops working. Notice that I used window.parent to gain access to the id of the icon, as it lies within the parent page which hosts the web resource.



Within CRM, a user can also use a key combination of Ctrl+S to commit some changes. If you like this functionality and you want this in your web resource too, here is a way to do it:


1 $(document).ready(function () {
2 //handle the Ctrl-S keypress
3 $(window).bind('keydown', function (event) {
4 if (event.ctrlKey || event.metaKey) {
5 switch (String.fromCharCode(event.which).toLowerCase()) {
6 case 's':
7 event.preventDefault();
8 TrySaveAll(); // my save method
9 break;
10 }
11 }
12 });
13 })

That´s it. Just handle the keydown event and do not forget to prevent the default action from happening.

No comments:

Post a Comment