Thursday, 28 June 2012

SharePoint 2010 Ribbon Button

Add New Custom Button on ListItem Ribbon

1. Display an alert message in the button action using javascript

Code Snippet:

    <CustomAction
      Id="CustomRibbonButton"
      RegistrationId="100"
      RegistrationType="List"
      Location="CommandUI.Ribbon"
      Sequence="10"
      Title="CustomRibbonButton">
        <CommandUIExtension>
            <CommandUIDefinitions>
                <CommandUIDefinition Location="Ribbon.ListItem.Manage.Controls._children">
                    <Button
                        Id="Ribbon.ListItem"
                        Alt="CustomRibbon"
                        Sequence="10"
                        Command="CustomButton"
                        LabelText ="Custom Button"
                        Image16by16="/_layouts/images/actionseditpage16.gif"
                        Image32by32="/_layouts/images/actionssettings.gif"
                        TemplateAlias="o1" />
                </CommandUIDefinition>
            </CommandUIDefinitions>
            <CommandUIHandlers>
                <CommandUIHandler Command="CustomButton"
                                  CommandAction="javascript: alert('My Command!');" />
            </CommandUIHandlers>
        </CommandUIExtension>
    </CustomAction>

Snapshot













2. Enable custom ribbon button after select an item

Change the below code
<CommandUIHandlers>
                <CommandUIHandler Command="CustomButton"
                                  EnabledScript="javascript: var EnableDisable = function() {  this.clientContext = SP.ClientContext.get_current();  this.selectedItems = SP.ListOperation.Selection.getSelectedItems(this.clientContext);  var ci = CountDictionary(selectedItems);  return (ci > 0);  };  EnableDisable();"
                                  CommandAction="javascript: alert('My Command!');" />
</CommandUIHandlers>


Snapshot


















3. Calling an Application page in Custom Button Action

Code snippet:

    <CustomAction
        Id="SkynetCustomRibbonButton"
        RegistrationId="100"
        RegistrationType="List"
        Location="CommandUI.Ribbon"
        Sequence="5"
        Title="CustomRibbon">

        <CommandUIExtension>
            <CommandUIDefinitions>
                <CommandUIDefinition Location="Ribbon.ListItem.Manage.Controls._children">
                    <Button
                        Id="Ribbon.ListItem"
                        Alt="CustomRibbon"
                        Sequence="5"
                        Command="Skynet_Test_Button"
                        Image16by16="/_layouts/images/actionseditpage16.gif"
                        Image32by32="/_layouts/images/actionssettings.gif"
                        LabelText ="Custom Button"
                        TemplateAlias="o1" />
                </CommandUIDefinition>

            </CommandUIDefinitions>

            <CommandUIHandlers>
                <CommandUIHandler
                Command="Skynet_Test_Button"
                CommandAction="javascript: function resultCallback(result, value) 
  {
  SP.UI.Notify.addNotification('Success!'); 
  SP.UI.ModalDialog.RefreshPage(SP.UI.DialogResult.OK); 
  }
  var ctx = SP.ClientContext.get_current();
  var items = SP.ListOperation.Selection.getSelectedItems(ctx);
  var myItems = '';
  var i;  
  for (i in items)
  {
  myItems += '|' + items[i].id;
  }  
  if(myItems != '') 
  { 
  var options =
  { 
  url: '/_layouts/CustomRibbonButton/sampleApplnPage.aspx?Items=' + myItems , 
  tite: 'My Page', 
  allowMaximize: false, 
  showClose: true,
  width: 500, 
  height: 160, 
  dialogReturnValueCallback: resultCallback
  };  
  SP.UI.ModalDialog.showModalDialog(options);
  }" />

            </CommandUIHandlers>
        </CommandUIExtension>
    </CustomAction>

Snapshot













4. Show custom button based on the rights

Scenario: Here my scenario is, the custom button should show only users who has full control.

Code Snippet:

do the below changes, add rights in Custom Action as follows

<CustomAction Rights="ManageWeb"
        Id="SkynetCustomRibbonButton"
        RegistrationId="100"
        RegistrationType="List"
        Location="CommandUI.Ribbon"
        Sequence="5"
        Title="CustomRibbon">

Snapshot

User with out Full Control














Full Control User
















Source download links

Link 1

Link 2

Link 3

Link 4


No comments:

Post a Comment