Invoking Code Behind from SharePoint Ribbon

sharepointsharepoint-2010

I've made a custom action button in my SharePoint ribbon and now I am trying to invoke some C# code behind from that button. I haven't been able to find any good details on how to do this. Does anyone know how to do this or have any good information on how to do this? Not sure it will help but this is my code for my custom action button. Thanks!

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">

  <CustomAction

    Id="CustomRibbonButton"
    RegistrationId="101"
    RegistrationType="List"
    Location="CommandUI.Ribbon"
    Sequence="5"
    Title="Move Documents">

    <CommandUIExtension>
      <CommandUIDefinitions>
        <CommandUIDefinition Location="Ribbon.Documents.Manage.Controls._children">
          <Button
              Id="Ribbon.Documents.New.MoveButton"
              Alt="Move Documents"
              Sequence="5"
              Command="Move_Button"
              Image32by32="/_layouts/images/Skynet/WinEarth32x32.png"
              Image16by16="/_layouts/images/Skynet/WinEarth16x16.png"
              LabelText="Move Documents"
              TemplateAlias="o1" />
        </CommandUIDefinition>
      </CommandUIDefinitions>

      <CommandUIHandlers>
        <CommandUIHandler
          Command="Move_Button"
          CommandAction="javascript:alert('SharePoint 2010 makes me angry!');" />
      </CommandUIHandlers>

    </CommandUIExtension>
  </CustomAction>

</Elements>

Best Answer

The CommandUIHandler can execute JavaScript (including the client side object model), but if you want to execute Server Object model code you can use the postback mechanism described in this MSDN article by Andrew Connell. He creates a webpart that listens to a postback event, that is generated by the ribbon button.

Alternatively, you can implement a webservice and call that from your JavaScript CommandUIHandler (ajax call). That approach is used here. (No longer accessible 2016-02-01)

Related Topic