Monday, February 27, 2012

Replacing a Button on the Server Ribbon in Sharepoint

in the MSDN article Walkthrough: Replacing a Button on the Server Ribbon

it is not as straight forward as it used to be. i can not complete it with only copy and paste the code into visual studio 2010 , then compile and deploy it to the SharePoint Server. I can't see the custom ribbon button since i use the incorrect registrationID.

first you will need to find the specific RegistrationID for Template Type that you want to custom Ribbon Tool Bar. from this link: SharePoint 2010: RegistrationId List Template Type IDs you can get the registrationID.

here is my sample code to test a button replaced with the default button.

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <CustomAction Id="Ribbon.Library.Actions.ReplacementButton"
    Location="CommandUI.Ribbon"  RegistrationId="115"
    RegistrationType="List"
    Title="Replace a Ribbon Button">
    <CommandUIExtension>
      <CommandUIDefinitions>
        <CommandUIDefinition
          Location="Ribbon.Library.Actions.ConnectToClient">
          <Button Id="Ribbon.Library.Actions.ConnectToClient.ReplaceNewButton"
            Command="ReplacementButtonCommand"
            Image16by16="/_Layouts/MyFeature/NEWITEM.GIF"
            Image32by32="/_LayoutsMyFeature/NEWITEM.GIF"
            LabelText="Replaced Button"
            TemplateAlias="o1" />
        </CommandUIDefinition>
      </CommandUIDefinitions>
      <!--<CommandUIHandlers>
        <CommandUIHandler
          Command="ReplacementButtonCommand"
          CommandAction="javascript:alert('This button has been replaced with the new One.');" />
      </CommandUIHandlers>-->
      <CommandUIHandlers>
        <CommandUIHandler
          Command="ReplacementButtonCommand"
          CommandAction="javascript:viewDialog('http://win-vq9f73d6i7j:28888/_layouts/FormServer.aspx?XsnLocation=http://win-vq9f73d6i7j:28888/RecyleItem/Forms/template.xsn&amp;SaveLocation=http%3A%2F%2Fwin%2Dvq9f73d6i7j%3A28888%2FRecyleItem&amp;ClientInstalled=true&amp;Source=http%3A%2F%2Fwin%2Dvq9f73d6i7j%3A28888%2FRecyleItem%2FForms%2FAllItems%2Easpx&amp;DefaultItemOpen=1')" />
      </CommandUIHandlers>
    </CommandUIExtension>
  </CustomAction>
  <CustomAction Id="Ribbon.Library.Actions.NewButton.Script"
    Location="ScriptLink"
    ScriptBlock="function viewDialog(linkUrl) { 
                  var options = {   
                      url: linkUrl,   
                      width: 800,   
                      height: 600, 
                  }; 
                  SP.UI.ModalDialog.showModalDialog(options);
              }"></CustomAction>
</Elements>


Please note that JavaScript can not recognize the & and i must replace it with "&amp;" otherwise
you will get the following errors

"Error    1    Entity 'DefaultItemOpen' not defined.    c:\users\administrator\documents\visual studio 2010\Projects\ReplaceARibbonButton\ReplaceARibbonButton\ReplaceARibbonButton\Elements.xml    27    357    ReplaceARibbonButton"

Error    2    Expecting ';'.    c:\users\administrator\documents\visual studio 2010\Projects\ReplaceARibbonButton\ReplaceARibbonButton\ReplaceARibbonButton\Elements.xml    27    372    ReplaceARibbonButton

No comments:

Post a Comment