0

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

HostWebDialog="TRUE"
HostWebDialogWidth="500"
HostWebDialogHeight="250"

And this to close with a button on the dialog box

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

So how to auto close?

EDIT

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()); });

EDIT2

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');
    selectedItems.update();
    clientContext.load(selectedItems);

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


}

$(document).ready(function () {

});

function OnFail() {
alert("Error");
}

It wont auto close

enter image description here

1

Wrap you closing code in a setTimeout

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

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

http://www.w3schools.com/jsref/met_win_settimeout.asp

Edit

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:

clientContext.executeQueryAsync(
    function(){
        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 – linkan94 Sep 30 '14 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 – Robert Lindgren Sep 30 '14 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 – linkan94 Sep 30 '14 at 14:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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