3

We are using SharePoint 2013 for intranet portal. Not apps.

We have a document library with documents. On clicking the document library we can see the list of documents. I want to place a clickable item on the ribbon of the doc library such that, when we click on a document, then I want to enable this button in ribbon and onclick redirect to an application page with the selected doc id.

I would also like to know the number of possible ways to do this and the best practice.

0

There are several ways of doing this:

Invoke server side code on SharePoint ribbon click using javascript submit

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 form = document.createElement("form");
    form.setAttribute("method", "post");
    form.setAttribute("action", pageUrl);
    document.body.appendChild(form);
    form.submit();   
    context.executeQueryAsync(Function.createDelegate(this, this.Success), Function.createDelegate(this, this.Failed));
}

Invoke server side code on SharePoint ribbon click using javascript _dopostback

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 + ",";
    }
    __doPostBack('RibbonDemo', itemIds + ";" + listId);    
    context.executeQueryAsync(Function.createDelegate(this, this.Success), Function.createDelegate(this, this.Failed));
}

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

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

See this for more info: http://sharepointnadeem.blogspot.com/2012/07/invoke-server-side-code-on-sharepoint.html

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.