I have ribbon button that open a dialog box. The thing is that when you click the ribbon button the dialog box should only flash and then close and refresh. I want to to it with javascript

I use this to get the dialog to show


And this to close with a button on the dialog box

window.parent.postMessage('CloseCustomActionDialogRefresh', '*');

So how to auto close?


Tried this but it didn't close I'm doing something wrong?

  clientContext.executeQueryAsync(setTimeout(function () { window.parent.postMessage('CloseCustomActionDialogRefresh', '*') }, 2000), function (sender, args) { alert('Error:' + args.get_message()); });


All my code:

'use strict';

var clientContext, hostweburl, parentContext, parentWeb, selectedItems, itemIds, listId;

clientContext = new SP.ClientContext.get_current();
hostweburl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
listId = decodeURIComponent(getQueryStringParameter("SPListId"));
itemIds = decodeURIComponent(getQueryStringParameter("SPListItemId")).split(',');
parentContext = new SP.AppContextSite(clientContext, hostweburl);
parentWeb = parentContext.get_web();
selectedItems = parentWeb.get_lists().getById(listId).getItemById(itemIds);

function itemLoop() {
var i;
for (i = 0; i < itemIds.length; i++) {
    selectedItems = parentWeb.get_lists().getById(listId).getItemById(itemIds[i]);
    selectedItems.set_item('Status', 'Completed');
    selectedItems.set_item('PercentComplete', '1');

clientContext.executeQueryAsync(function () { window.parent.postMessage('CloseCustomActionDialogRefresh', '*'); }, function (sender, args) { alert('Error:' + args.get_message()); });


$(document).ready(function () {


function OnFail() {

It wont auto close

enter image description here

1 Answer 1


Wrap you closing code in a setTimeout

   window.parent.postMessage('CloseCustomActionDialogRefresh', '*');
}, 2000);

The number 2000 indicates the number of milliseconds before the function will be executed



In your case, it seems you want to close when you have successfully executed a query? If so, do something like this, no need for a setTimeout:

        window.parent.postMessage('CloseCustomActionDialogRefresh', '*');
    function (sender, args) { 
        alert('Error:' + args.get_message()); 
  • yes when it has successfully executed a query but it dont auto close with that code and it's the auto close i need Commented Sep 30, 2014 at 14:37
  • That should auto close it (given that the code you provided as close-code actually works on its own, and that the executeQuery is successful Commented Sep 30, 2014 at 14:40
  • Ok weird it dosen't close anyway posted a pic do you know any other way to auto close it and the executeQuery is successful Commented Sep 30, 2014 at 14:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.