Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I want to create a Button in my Ribbon which modifies some fields in my items.. In details I want to select more than one element and select my "link" button and make the elements be "linked" by an integer field populated with the same value.

I already have other ribbon custom button but for those it was easy to work only with Javascript. Can anybody suggest me how to do this thing?

I read online someone suggest to create a custom WebControl invoked by "postback" to react to event... There is any other way or a good tutorial?

Thank you very much!

share|improve this question
add comment

4 Answers

up vote 2 down vote accepted

Use __doPostback method on the client:

__doPostback('myPostBack','');

Then, on server you should have something like this:

const string myPostBackId = "myPostBack";

public override CreateChildControls()
{
    // ...

    if (this.Page.Request["__EVENTTARGET"] == myPostbackId)
    {
        string textBoxValue = this.Page.Request["__EVENTARGUMENT"];
        // do whatever you need with textBoxValue
    }

    // ...
}

Also, I have two working examples for my Fluent Ribbon API opensource project, of how to work with postbacks. I think, exploring these examples could help, even if you don't intend to use Fluent Ribbon API. Here they are:

  1. Simple example from the project documentation site
  2. Article, which explains how to use Ribbon ToggleButton in conjunction with UpdatePanel to create multiview pages
share|improve this answer
add comment

You could also create an application page which you pass the GUIDs of the selected items via a JavaScript redirect. currentsite/_layouts/AppPage?GUIDS=guid1,guid2&ReturnURL=/Pages/currentpage.

Have your code complete the request then redirect to the list view via the ReturnUrl paramater.

share|improve this answer
add comment

Apart the suggestion wrote from Andrey and Anders I suggest to you another solution. By javascript call a wcf to do the work do you need.

share|improve this answer
add comment

There are several ways of doing it:

Invoke server side code on SharePoint ribbon click using javascript submit. Example:

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

Invoke server side code on SharePoint ribbon click using javascript _dopostback. Example:

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

function Success() {
    var theForm = document.forms[0];
    theForm.__EVENTTARGET.value = "";
    theForm.__EVENTARGUMENT.value = "";
}

function Failed(sender, args) {
    alert('request failed ' + args.get_message() + 'n' + args.get_stackTrace());
}

function RibbonDemoEnable() {   
    var context = SP.ClientContext.get_current();
    return SP.ListOperation.Selection.getSelectedItems(context).length >= 1;
}

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

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 information: http://sharepointnadeem.blogspot.com/2012/07/invoke-server-side-code-on-sharepoint.html

share|improve this answer
1  
While this may theoretically answer the question, we prefer inclusion of the essential parts of the answer here, and to provide the link for reference. See answer for general guidelines. Also we require relevant answers and disclosure of any affiliation with products or websites. Please see May I promote products or websites I am affiliated with here? for guidelines. Please fix this and flag for a moderator to review. Thanks. –  SPDoctor Jul 15 '12 at 8:37
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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