SharePoint: Invoke server side code on SharePoint ribbon click using SharePoint Page dialog


In this series we will look at three different ways of achieving this.
3. Invoke server side code on SharePoint ribbon click using SharePoint Page dialog

Solution:
Invoke server side code on SharePoint ribbon click using SharePoint Page dialog:

This is similar to the first example except that instead of submitting the form, the Layouts page is opened in as a modal Dialog like this:

function RibbonDemo() {
var context = SP.ClientContext.get_current();
    var selectedItems = SP.ListOperation.Selection.getSelectedItems(context);
    var listId = SP.ListOperation.Selection.getSelectedList();
    var itemIds = "";
    for (var i = 0; i < selectedItems.length; i++) {
        itemIds += selectedItems[i].id + ",";
    }
    var pageUrl = SP.Utilities.Utility.getLayoutsPageUrl(
        '/SharePointRibbonDemo/RibbonDemo.aspx?ids=' + itemIds + '&listId=' + listId);
    var options = SP.UI.$create_DialogOptions();
    options.width = 500;
    options.height = 300;
    options.url = pageUrl;
    options.dialogReturnValueCallback = Function.createDelegate(null, OnDialogClose);
    SP.UI.ModalDialog.showModalDialog(options);
}
function OnDialogClose(result, target) {
    if (result == SP.UI.DialogResult.OK) {
        alert('OK was clicked");
}
}
profile for Nadeem Yousuf at SharePoint Stack Exchange, Q&A for SharePoint enthusiasts

+