quinta-feira, Abril 24, 2014

How to Create Expand Collapse Left Menu using Scriptlink Custom Action

This a simple article on how we can use Jquery and  Custom Actions to customize Look and feel in a collaborative Site, in this case  Customize the Left Menu". 
The main idea here is to use Jquery to create a Expand and collapse Left Menu and save the state (Expand/ Collapse) using Cookies.

I believe there is tons of articles talking about this, but let me try with this way, maybe you liked or not, let see.

There are multiple ways of make this change, since change Masterpages and Styles, add embedded code "Script Editor", but when you work for a corporation, change Masterpage is never a good approach, not even possible by the policies of Company/Corporation....
Add Web Part with code, it is not very recommended because that required a loot of work "needs to be added in every page" and "garbage" in the Web Part page, not very flexible indeed .

To avoid touch the current environment and manage the loading of out Script like to recommend the ScriptLink Custom Action to add globally in a Site Collection or Site. I can recommend the following SharePoint app to Manage this Actions.

Before the Solution is implemented 

The following image define the area that should be manage and shown when requested.



After the solution is implemented a new option should appear in the Left Menu, this should used to hide and expand the content of the Left Menu.

Left Menu Expanded (animated)



Left Menu Collapsed (animated)



How to implement the Solution

This solution is consisted in 4 files added in SharePoint Libraries and 2 SharePoint Custom Actions as described bellow.
This files are used to manage the visibility of the Left Menu. 

Files

  • Jquery.js
  • ExpandCollapse.js
  • Left Image
  • Right Image
SharePoint Custom Action
  • Global Jquery
    • includes Jquery in Masterpage Header using MDS
  • Expand and collapse
    •  includes Jquery in Masterpage Header using MDS

Include Jquery Script in SharePoint Document Library


Access to Site Assets Library and add the Jquery file, you can use the following link to download.
This Solution uses Jquery and "animateMethod to expand and collapse the Left Menu.


Add Scriptlink Custom Action "Global Jquery" in SharePoint Site

After you include the Jquery plug-in in all pages of your SharePoint Site you can use the recommend tool and add the Custom Action ScriptLink. 



If you like to use Visual Studio and Declarative code you can use the option "Generate Declarative XML" to create the XML for your SharePoint Feature as shown.

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <CustomAction Id="Global Jquery" Title="Global Jquery" Description="" Location="ScriptLink" ScriptSrc="~site/SiteAssets/jquery-1.10.2.min.js" Sequence="1" > </CustomAction>

</Elements>


Include Script to Expand/Collapse Left Menu in SharePoint Site

This script should make the management of the Left Menu visibility in all pages where the Left Menu is used. 

Include Expand/Collapse Script in SharePoint Library

If The Script is separated in different Methods as described:

  • OnLoad (Validate Cookie and Load Image to Expand/Collapse)
  • CloseLeftMenu (Close Left Menu Area)
  • OpenLeftMenu (Expand Left Menu Area)
  • Getcookie (get Cookie "HideLeftMenu")

Script that needs to be save in the ExpandCollapse.js file:

var LeftImage = "";
var RightImage = "";

//Validate Cookie and Expand/collapse
$(document).ready(function () {
    LeftImage = _spPageContextInfo.webServerRelativeUrl + ((_spPageContextInfo.webServerRelativeUrl.indexOf('/', _spPageContextInfo.webServerRelativeUrl.length - 1) !== -1) ? '' : '/') + 'SiteAssets/Arrowhead-Left-01.png';
    RightImage = _spPageContextInfo.webServerRelativeUrl + ((_spPageContextInfo.webServerRelativeUrl.indexOf('/', _spPageContextInfo.webServerRelativeUrl.length - 1) !== -1) ? '' : '/') + 'SiteAssets/Arrowhead-Right-01.png';

    if (getCookie('HideLeftMenu') == 'true') {
        $('#sideNavBox').prepend("<div align='Right'><img id='LeftMenuico' src='" + RightImage + "' /></div>");
        CloseLeftMenu();
    }
    else {
        $('#sideNavBox').prepend("<div align='Right'><img id='LeftMenuico' src='" + LeftImage + "' /></div>");
        if ($('#contentBox').css('margin-left') == "20px") {
            OpenLeftMenu();
        }
        else {
            $("#LeftMenuico").unbind();
            $("#LeftMenuico").click(function () {
                CloseLeftMenu();
            });
        }
    }
});

//Collapse Method
function CloseLeftMenu() {
    document.cookie = "HideLeftMenu=true";
    $('#sideNavBox').css('position''absolute');
    $('#sideNavBox').animate({ left: '-=180' }, 300);
    $('#contentBox').css('margin-left''20px');
    $('#LeftMenuico').attr('src', RightImage);
    $("#LeftMenuico").unbind();
    $("#LeftMenuico").click(function () {
        OpenLeftMenu();
    });
}
//Expand Method
function OpenLeftMenu() {
    document.cookie = "HideLeftMenu=false";
    $('#sideNavBox').css('position''absolute');
    $('#sideNavBox').animate({ left: '+=180' }, 300);
    $('#contentBox').css('margin-left''220px');
    $('#LeftMenuico').attr('src', LeftImage);
    $("#LeftMenuico").unbind();
    $("#LeftMenuico").click(function () {
        CloseLeftMenu();
    });
}
//Get Cookie Method
function getCookie(cname) {
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i].trim();
        if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
    }
    return "";

}

After the Script is created you can include in the Library "Site Assets" and include references to Left and Right image Option as display in the images in top of the Article.


Add Scriptlink Custom Action "Expand and Collapse" in SharePoint Site

Same process to include as Jquery but BE CAREFUL with the SEQUENCE Order has to be after the Jquery Plugin. 



If you like to use Visual Studio and Declarative code you can use the option "Generate Declarative XML" to create the XML for your SharePoint Feature as shown.


<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <CustomAction Id="Expand and Collapse" Title="Expand and Collapse" Description="" Location="ScriptLink" ScriptSrc="~site/SiteAssets/ExpandCollapse.js" Sequence="2" > </CustomAction>

</Elements>

After this is changed, should access to your SharePoint Site and validate if the new option is working correctly as shown in the images.


 

To get the cookie from the LeftMenu Status (Expand/collapse) you can use Internet Explorer “Developer Tools” and use Network to sniff the traffic and get the cookie in the Site.  



Ok, this is everything you should know, as you can see, it is very easy make some Customization in your site without change anything in the Masterpages or CSS.

Hope you like this article,
Kind regards, 
André Lage

terça-feira, Abril 22, 2014

Examples of Ribbons Customization in SharePoint 2013 - link

Examples of Ribbons Customization in SharePoint 2013

The Customization of ribbons and his behavior continues to be a big headache to developer and Site Administrators that need to make some customization in their Site.
This article provides some examples of Ribbon XML Structure you can use to customize your SharePoint Lists/Libraries.  

Customizing Ribbons
Customization of a Ribbon can be included in SharePoint sites using Custom Actions associated in the UI Extension using XML format. This Structure can be included in Visual Studio using structure file "Element.xml" and deploy as solution (SP App, SP Sandbox or SP Farm) or using SharePoint Object UserCustomAction .




PS: You can also include Ribbon Action using SharePoint JavaScript Object Model (SP.Ribbon.JS) but in local pages.

You can read articles about add Custom Action with Ribbons in a declarative way or add in JSOM way.

The following Table are the main Location will be located the Custom Ribbons when the User Custom Action is created.

Locations
Description
CommandUI.Ribbon
Location corresponds to the list view and edit, new, and display forms for the list
CommandUI.Ribbon.ListView
Location corresponds to the list view.
CommandUI.Ribbon.EditForm
Location corresponds to the edit form for the list.
CommandUI.Ribbon.NewForm
Location corresponds to the new form for the list.
CommandUI.Ribbon.DisplayForm
Location corresponds to the display form for the list.

The following 

XML Ribbon examples :
  • Add Custom Tab
  • Add Custom Group to Custom Tab
  • Add Custom Group Template with Checkbox Control
  • Add Custom Control in Custom Group and Custom Tab
  • Add Custom Group/Control (Large) in existing Tab
  • Add Custom Group/Control (Large/Medium) in existing Tab
  • Add FlyoutAnchor in Custom Tab/Custom Group
  • Add Button Control in Existing Tab/Group
  • Hide existing Ribbon control
  • Replace existing Ribbon control
Tab

This option will help customizing Ribbons in lists/Libraries creating a custom Tab that responses specific Business rules and isolates from the existing SharePoint Out of the box Ribbons Controls. 

Add Custom Tab

This example create a custom Tab but will not be shown because there isn't a Group associated.

Custom Tab Example:

<CommandUIExtension xmlns="http://schemas.microsoft.com/sharepoint/">
  <CommandUIDefinitions>
    <CommandUIDefinition Location="Ribbon.Tabs._children">
      <Tab Id="My Custom Tab" Title="My Custom Tab" Description="My Custom Tab" Sequence="0">
        <Scaling Id="My Custom Tab.Scaling">
        </Scaling>
        <Groups Id="My Custom Tab.Groups">
        </Groups>
      </Tab>
    </CommandUIDefinition>
  </CommandUIDefinitions>
</CommandUIExtension>

Group 

This option defines the area and layout on how the controls will be displayed, this is a very important section because will have impact in the position and size of the Custom Controls in the Ribbon.

Add Custom Group to Custom Tab

This option uses a custom Group Template call Flexible2 to be associated with the created Custom Tab.

<CommandUIExtension xmlns="http://schemas.microsoft.com/sharepoint/">
  <CommandUIDefinitions>
    <CommandUIDefinition Location="Ribbon.Tabs._children">
      <Tab Id="My Custom Tab" Title="My Custom Tab" Description="My Custom Tab" Sequence="0">
        <Scaling Id="My Custom Tab.Scaling">
          <MaxSize Id="My Group.Scaling.MaxSize" GroupId="My Group" Size="LargeLarge" />
          <Scale Id="My Group.Scaling.Scale" GroupId="My Group" Size="LargeLarge" />
        </Scaling>
        <Groups Id="My Custom Tab.Groups">
          <Group Id="My Group" Title="My Group" Template="Ribbon.Templates.Flexible2" Command="My Group.Command">
            <Controls Id="My Group.Controls">
            </Controls>
          </Group>
        </Groups>
      </Tab>
    </CommandUIDefinition>
    <CommandUIDefinition Location="Ribbon.Templates._children">
      <GroupTemplate Id="Ribbon.Templates.Flexible2">
        <Layout Title="LargeLarge" LayoutTitle="LargeLarge">
          <OverflowSection DisplayMode="Large" TemplateAlias="o1" Type="OneRow" />
          <OverflowSection DisplayMode="Large" TemplateAlias="o2" Type="OneRow" />
        </Layout>
        <Layout Title="LargeMedium" LayoutTitle="LargeMedium">
          <OverflowSection DisplayMode="Large" TemplateAlias="o1" Type="OneRow" />
          <OverflowSection DisplayMode="Medium" TemplateAlias="o2" Type="ThreeRow" />
        </Layout>
        <Layout Title="LargeSmall" LayoutTitle="LargeSmall">
          <OverflowSection DisplayMode="Large" TemplateAlias="o1" Type="OneRow" />
          <OverflowSection DisplayMode="Small" TemplateAlias="o2" Type="ThreeRow" />
        </Layout>
        <Layout Title="MediumLarge" LayoutTitle="MediumLarge">
          <OverflowSection DisplayMode="Medium" TemplateAlias="o1" Type="ThreeRow" />
          <OverflowSection DisplayMode="Large" TemplateAlias="o2" Type="OneRow" />
        </Layout>
        <Layout Title="MediumMedium" LayoutTitle="MediumMedium">
          <OverflowSection DisplayMode="Medium" TemplateAlias="o1" Type="ThreeRow" />
          <OverflowSection DisplayMode="Medium" TemplateAlias="o2" Type="ThreeRow" />
        </Layout>
        <Layout Title="MediumSmall" LayoutTitle="MediumSmall">
          <OverflowSection DisplayMode="Medium" TemplateAlias="o1" Type="ThreeRow" />
          <OverflowSection DisplayMode="Small" TemplateAlias="o2" Type="ThreeRow" />
        </Layout>
        <Layout Title="SmallLarge" LayoutTitle="SmallLarge">
          <OverflowSection DisplayMode="Small" TemplateAlias="o1" Type="ThreeRow" />
          <OverflowSection DisplayMode="Large" TemplateAlias="o2" Type="OneRow" />
        </Layout>
        <Layout Title="SmallMedium" LayoutTitle="SmallMedium">
          <OverflowSection DisplayMode="Small" TemplateAlias="o1" Type="ThreeRow" />
          <OverflowSection DisplayMode="Medium" TemplateAlias="o2" Type="ThreeRow" />
        </Layout>
        <Layout Title="SmallSmall" LayoutTitle="SmallSmall">
          <OverflowSection DisplayMode="Small" TemplateAlias="o1" Type="ThreeRow" />
          <OverflowSection DisplayMode="Small" TemplateAlias="o2" Type="ThreeRow" />
        </Layout>
      </GroupTemplate>
    </CommandUIDefinition>
  </CommandUIDefinitions>
  <CommandUIHandlers>
    <CommandUIHandler Command="My Group.Command" CommandAction="javascript:return true;" />
  </CommandUIHandlers>
</CommandUIExtension>

The final output of the Ribbon should be:


Group Template Options

Ok,  when we are customizing Ribbons the creation of Group template is the most hard thing to be defined.
This will be the area where your controls will be organize and displayed in the Ribbon, this design area can have multiple format and ways to be displayed, it is recommended a good evaluation on how the ribbons will be displayed and then design.
Example of Custom Group Template Structure:
This example came from processlynx SharePoint app (Ribbon)


For all the examples except "Create Custom Group Template with Checkbox Control" was created a  Custom Template call "Flexible2" this Template has multiple layouts that helps the customization of the Ribbons areas and how is displayed. 

Here some examples of possible Custom Group template Layouts you can create or design:


or 

Here a example of Custom Template Flexible2 XML with multiple layouts:


 <CommandUIDefinition Location="Ribbon.Templates._children">
      <GroupTemplate Id="Ribbon.Templates.Flexible2">
        <Layout Title="LargeLarge" LayoutTitle="LargeLarge">
          <OverflowSection DisplayMode="Large" TemplateAlias="o1" Type="OneRow" />
          <OverflowSection DisplayMode="Large" TemplateAlias="o2" Type="OneRow" />
        </Layout>
        <Layout Title="LargeMedium" LayoutTitle="LargeMedium">
          <OverflowSection DisplayMode="Large" TemplateAlias="o1" Type="OneRow" />
          <OverflowSection DisplayMode="Medium" TemplateAlias="o2" Type="ThreeRow" />
        </Layout>
        <Layout Title="LargeSmall" LayoutTitle="LargeSmall">
          <OverflowSection DisplayMode="Large" TemplateAlias="o1" Type="OneRow" />
          <OverflowSection DisplayMode="Small" TemplateAlias="o2" Type="ThreeRow" />
        </Layout>
        <Layout Title="MediumLarge" LayoutTitle="MediumLarge">
          <OverflowSection DisplayMode="Medium" TemplateAlias="o1" Type="ThreeRow" />
          <OverflowSection DisplayMode="Large" TemplateAlias="o2" Type="OneRow" />
        </Layout>
        <Layout Title="MediumMedium" LayoutTitle="MediumMedium">
          <OverflowSection DisplayMode="Medium" TemplateAlias="o1" Type="ThreeRow" />
          <OverflowSection DisplayMode="Medium" TemplateAlias="o2" Type="ThreeRow" />
        </Layout>
        <Layout Title="MediumSmall" LayoutTitle="MediumSmall">
          <OverflowSection DisplayMode="Medium" TemplateAlias="o1" Type="ThreeRow" />
          <OverflowSection DisplayMode="Small" TemplateAlias="o2" Type="ThreeRow" />
        </Layout>
        <Layout Title="SmallLarge" LayoutTitle="SmallLarge">
          <OverflowSection DisplayMode="Small" TemplateAlias="o1" Type="ThreeRow" />
          <OverflowSection DisplayMode="Large" TemplateAlias="o2" Type="OneRow" />
        </Layout>
        <Layout Title="SmallMedium" LayoutTitle="SmallMedium">
          <OverflowSection DisplayMode="Small" TemplateAlias="o1" Type="ThreeRow" />
          <OverflowSection DisplayMode="Medium" TemplateAlias="o2" Type="ThreeRow" />
        </Layout>
        <Layout Title="SmallSmall" LayoutTitle="SmallSmall">
          <OverflowSection DisplayMode="Small" TemplateAlias="o1" Type="ThreeRow" />
          <OverflowSection DisplayMode="Small" TemplateAlias="o2" Type="ThreeRow" />
        </Layout>
      </GroupTemplate>
    </CommandUIDefinition>


Add Custom Group Template with Checkbox Control

This option uses a custom Group Template call Flexible2 to be associated with the created Custom Tab.

<CommandUIExtension xmlns="http://schemas.microsoft.com/sharepoint/">
  <CommandUIDefinitions>
    <CommandUIDefinition Location="Ribbon.Templates._children">
      <GroupTemplate Id="Custom Group Template">
        <Layout Title="Custom Group Template Layout" LayoutTitle="Custom Group Template Layout">
          <Section Alignment="Top" Type="OneRow">
            <Row>
              <ControlRef DisplayMode="Small" TemplateAlias="Custom Group Template Layout Alias 1" />
            </Row>
          </Section>
        </Layout>
      </GroupTemplate>
    </CommandUIDefinition>
    <CommandUIDefinition Location="Ribbon.Library.Groups._children">
      <Group Id="Custom Group with Custom Template" Title="Custom Group with Custom Template" Template="Custom Group Template" Command="Custom Group with Custom Template.Command">
        <Controls Id="Custom Group with Custom Template.Controls">
          <CheckBox Id="Custom Group with Custom Template Button" LabelText="Custom Group with Custom Template Button" Command="Custom Group with Custom Template Button.Command" TemplateAlias="Custom Group Template Layout Alias 1" />
        </Controls>
      </Group>
    </CommandUIDefinition>
    <CommandUIDefinition Location="Ribbon.Library.Scaling._children">
      <MaxSize Id="Custom Group with Custom Template.Scaling.MaxSize" GroupId="Custom Group with Custom Template" Size="Custom Group Template Layout" />
    </CommandUIDefinition>
  </CommandUIDefinitions>
  <CommandUIHandlers>
    <CommandUIHandler Command="Custom Group with Custom Template.Command" CommandAction="javascript:return true;" />
    <CommandUIHandler Command="Custom Group with Custom Template Button.Command" CommandAction="javascript:(alert('Custom Action');)" />
  </CommandUIHandlers>
</CommandUIExtension>


The final output of the Ribbon should be:


The visual representation of our Ribbon should be the following tree.
  • Creation of the Custom Group Template and definition of his content
    • After you create the Custom Template you can create multiple layouts to hum
    • Each layout could have multiple designs and areas.
  • Create a Custom Group using as Base the Custom Template created in "Custom Group Template", add control Checkbox  using the Layout Small "Check box option can only be added with small or medium"  and the Ribbon should be created.

This example came from processlynx SharePoint app (Ribbon)


Control Option

This option give the ability to create custom Controls associating actions as links or JavaScript that can be associated with a custom Layout.

Add Custom Control in Custom Group and Custom Tab

This example add a Custom Large Control in a Custom Tab and Custom Group.

The XML structure defines the Tab > Group > Controls that should appear in the Ribbon.


<Tab Id="My Custom Tab" Title="My Custom Tab" Description="My Custom Tab" Sequence="0">
        <Scaling Id="My Custom Tab.Scaling">
          <MaxSize Id="My Group.Scaling.MaxSize" GroupId="My Group" Size="LargeLarge" />
          <Scale Id="My Group.Scaling.Scale" GroupId="My Group" Size="LargeLarge" />
        </Scaling>
        <Groups Id="My Custom Tab.Groups">
          <Group Id="My Group" Title="My Group" Template="Ribbon.Templates.Flexible2" Command="My Group.Command">
            <Controls Id="My Group.Controls">
              <Button Id="My Button" LabelText="My Button" Image16by16="/_layouts/15/1033/images/formatmap16x16.png?rev=33" Image16by16Left="-263" Image16by16Top="-2" Image32by32="/_layouts/15/1033/images/formatmap32x32.png?rev=33" Image32by32Left="-510" Image32by32Top="-0" Command="My Button.Command" TemplateAlias="o1" />
            </Controls>
          </Group>
        </Groups>
      </Tab>

Here is the full XML code example:

<CommandUIExtension xmlns="http://schemas.microsoft.com/sharepoint/">
  <CommandUIDefinitions>
    <CommandUIDefinition Location="Ribbon.Tabs._children">
      <Tab Id="My Custom Tab" Title="My Custom Tab" Description="My Custom Tab" Sequence="0">
        <Scaling Id="My Custom Tab.Scaling">
          <MaxSize Id="My Group.Scaling.MaxSize" GroupId="My Group" Size="LargeLarge" />
          <Scale Id="My Group.Scaling.Scale" GroupId="My Group" Size="LargeLarge" />
        </Scaling>
        <Groups Id="My Custom Tab.Groups">
          <Group Id="My Group" Title="My Group" Template="Ribbon.Templates.Flexible2" Command="My Group.Command">
            <Controls Id="My Group.Controls">
              <Button Id="My Button" LabelText="My Button" Image16by16="/_layouts/15/1033/images/formatmap16x16.png?rev=33" Image16by16Left="-263" Image16by16Top="-2" Image32by32="/_layouts/15/1033/images/formatmap32x32.png?rev=33" Image32by32Left="-510" Image32by32Top="-0" Command="My Button.Command" TemplateAlias="o1" />
            </Controls>
          </Group>
        </Groups>
      </Tab>
    </CommandUIDefinition>
    <CommandUIDefinition Location="Ribbon.Templates._children">
      <GroupTemplate Id="Ribbon.Templates.Flexible2">
        <Layout Title="LargeLarge" LayoutTitle="LargeLarge">
          <OverflowSection DisplayMode="Large" TemplateAlias="o1" Type="OneRow" />
          <OverflowSection DisplayMode="Large" TemplateAlias="o2" Type="OneRow" />
        </Layout>
        <Layout Title="LargeMedium" LayoutTitle="LargeMedium">
          <OverflowSection DisplayMode="Large" TemplateAlias="o1" Type="OneRow" />
          <OverflowSection DisplayMode="Medium" TemplateAlias="o2" Type="ThreeRow" />
        </Layout>
        <Layout Title="LargeSmall" LayoutTitle="LargeSmall">
          <OverflowSection DisplayMode="Large" TemplateAlias="o1" Type="OneRow" />
          <OverflowSection DisplayMode="Small" TemplateAlias="o2" Type="ThreeRow" />
        </Layout>
        <Layout Title="MediumLarge" LayoutTitle="MediumLarge">
          <OverflowSection DisplayMode="Medium" TemplateAlias="o1" Type="ThreeRow" />
          <OverflowSection DisplayMode="Large" TemplateAlias="o2" Type="OneRow" />
        </Layout>
        <Layout Title="MediumMedium" LayoutTitle="MediumMedium">
          <OverflowSection DisplayMode="Medium" TemplateAlias="o1" Type="ThreeRow" />
          <OverflowSection DisplayMode="Medium" TemplateAlias="o2" Type="ThreeRow" />
        </Layout>
        <Layout Title="MediumSmall" LayoutTitle="MediumSmall">
          <OverflowSection DisplayMode="Medium" TemplateAlias="o1" Type="ThreeRow" />
          <OverflowSection DisplayMode="Small" TemplateAlias="o2" Type="ThreeRow" />
        </Layout>
        <Layout Title="SmallLarge" LayoutTitle="SmallLarge">
          <OverflowSection DisplayMode="Small" TemplateAlias="o1" Type="ThreeRow" />
          <OverflowSection DisplayMode="Large" TemplateAlias="o2" Type="OneRow" />
        </Layout>
        <Layout Title="SmallMedium" LayoutTitle="SmallMedium">
          <OverflowSection DisplayMode="Small" TemplateAlias="o1" Type="ThreeRow" />
          <OverflowSection DisplayMode="Medium" TemplateAlias="o2" Type="ThreeRow" />
        </Layout>
        <Layout Title="SmallSmall" LayoutTitle="SmallSmall">
          <OverflowSection DisplayMode="Small" TemplateAlias="o1" Type="ThreeRow" />
          <OverflowSection DisplayMode="Small" TemplateAlias="o2" Type="ThreeRow" />
        </Layout>
      </GroupTemplate>
    </CommandUIDefinition>
  </CommandUIDefinitions>
  <CommandUIHandlers>
    <CommandUIHandler Command="My Group.Command" CommandAction="javascript:return true;" />
    <CommandUIHandler Command="My Button.Command" CommandAction="javascript:alert('My Action');" />
  </CommandUIHandlers>
</CommandUIExtension>

The final output of the Ribbon should be:


Add Custom Group/Control (Large) in existing Tab

This example add a Custom Large Control in a existing Tab but in a Custom Group.

<CommandUIExtension xmlns="http://schemas.microsoft.com/sharepoint/">
  <CommandUIDefinitions>
    <CommandUIDefinition Location="Ribbon.Library.Groups._children">
      <Group Id="My Custom Group in Existing Tab" Title="My Custom Group in Existing Tab" Template="Ribbon.Templates.Flexible2" Command="My Custom Group in Existing Tab.Command">
        <Controls Id="My Custom Group in Existing Tab.Controls">
          <Button Id="My Custom Button" LabelText="My Custom Button" Image16by16="/_layouts/15/1033/images/formatmap16x16.png?rev=33" Image16by16Left="-163" Image16by16Top="-218" Image32by32="/_layouts/15/1033/images/formatmap32x32.png?rev=33" Image32by32Left="-270" Image32by32Top="-170" Command="My Custom Button.Command" TemplateAlias="o1" />
        </Controls>
      </Group>
    </CommandUIDefinition>
    <CommandUIDefinition Location="Ribbon.Library.Scaling._children">
      <MaxSize Id="My Custom Group in Existing Tab.Scaling.MaxSize" GroupId="My Custom Group in Existing Tab" Size="LargeLarge" />
    </CommandUIDefinition>
    <CommandUIDefinition Location="Ribbon.Templates._children">
      <GroupTemplate Id="Ribbon.Templates.Flexible2">
        <Layout Title="LargeLarge" LayoutTitle="LargeLarge">
          <OverflowSection DisplayMode="Large" TemplateAlias="o1" Type="OneRow" />
          <OverflowSection DisplayMode="Large" TemplateAlias="o2" Type="OneRow" />
        </Layout>
        <Layout Title="LargeMedium" LayoutTitle="LargeMedium">
          <OverflowSection DisplayMode="Large" TemplateAlias="o1" Type="OneRow" />
          <OverflowSection DisplayMode="Medium" TemplateAlias="o2" Type="ThreeRow" />
        </Layout>
        <Layout Title="LargeSmall" LayoutTitle="LargeSmall">
          <OverflowSection DisplayMode="Large" TemplateAlias="o1" Type="OneRow" />
          <OverflowSection DisplayMode="Small" TemplateAlias="o2" Type="ThreeRow" />
        </Layout>
        <Layout Title="MediumLarge" LayoutTitle="MediumLarge">
          <OverflowSection DisplayMode="Medium" TemplateAlias="o1" Type="ThreeRow" />
          <OverflowSection DisplayMode="Large" TemplateAlias="o2" Type="OneRow" />
        </Layout>
        <Layout Title="MediumMedium" LayoutTitle="MediumMedium">
          <OverflowSection DisplayMode="Medium" TemplateAlias="o1" Type="ThreeRow" />
          <OverflowSection DisplayMode="Medium" TemplateAlias="o2" Type="ThreeRow" />
        </Layout>
        <Layout Title="MediumSmall" LayoutTitle="MediumSmall">
          <OverflowSection DisplayMode="Medium" TemplateAlias="o1" Type="ThreeRow" />
          <OverflowSection DisplayMode="Small" TemplateAlias="o2" Type="ThreeRow" />
        </Layout>
        <Layout Title="SmallLarge" LayoutTitle="SmallLarge">
          <OverflowSection DisplayMode="Small" TemplateAlias="o1" Type="ThreeRow" />
          <OverflowSection DisplayMode="Large" TemplateAlias="o2" Type="OneRow" />
        </Layout>
        <Layout Title="SmallMedium" LayoutTitle="SmallMedium">
          <OverflowSection DisplayMode="Small" TemplateAlias="o1" Type="ThreeRow" />
          <OverflowSection DisplayMode="Medium" TemplateAlias="o2" Type="ThreeRow" />
        </Layout>
        <Layout Title="SmallSmall" LayoutTitle="SmallSmall">
          <OverflowSection DisplayMode="Small" TemplateAlias="o1" Type="ThreeRow" />
          <OverflowSection DisplayMode="Small" TemplateAlias="o2" Type="ThreeRow" />
        </Layout>
      </GroupTemplate>
    </CommandUIDefinition>
  </CommandUIDefinitions>
  <CommandUIHandlers>
    <CommandUIHandler Command="My Custom Group in Existing Tab.Command" CommandAction="javascript:return true;" />
    <CommandUIHandler Command="My Custom Button.Command" CommandAction="javascript:alert('My custom Action')" />
  </CommandUIHandlers>
</CommandUIExtension>

The final output of the Ribbon should be:


Add Custom Group/Control (Large/Medium) in existing Tab

This example add a Custom Large Control and medium control in a Row in a existing Tab but in a Custom Group.

Here is the full XML code example:

<CommandUIExtension xmlns="http://schemas.microsoft.com/sharepoint/">
  <CommandUIDefinitions>
    <CommandUIDefinition Location="Ribbon.Library.Groups._children">
      <Group Id="My Custom Group in Existing Tab" Title="My Custom Group in Existing Tab" Template="Ribbon.Templates.Flexible2" Command="My Custom Group in Existing Tab.Command">
        <Controls Id="My Custom Group in Existing Tab.Controls">
          <Button Id="My Custom Button" LabelText="My Custom Button" Image16by16="/_layouts/15/1033/images/formatmap16x16.png?rev=33" Image16by16Left="-163" Image16by16Top="-218" Image32by32="/_layouts/15/1033/images/formatmap32x32.png?rev=33" Image32by32Left="-270" Image32by32Top="-170" Command="My Custom Button.Command" TemplateAlias="o1" />
          <Button Id="My Custom Button 1" LabelText="My Custom Button 1" Image16by16="/_layouts/15/1033/images/formatmap16x16.png?rev=33" Image16by16Left="-303" Image16by16Top="-1" Image32by32="/_layouts/15/1033/images/formatmap32x32.png?rev=33" Image32by32Left="-372" Image32by32Top="-3" Command="My Custom Button 1.Command" TemplateAlias="o2" />
        </Controls>
      </Group>
    </CommandUIDefinition>
    <CommandUIDefinition Location="Ribbon.Library.Scaling._children">
      <MaxSize Id="My Custom Group in Existing Tab.Scaling.MaxSize" GroupId="My Custom Group in Existing Tab" Size="LargeMedium" />
    </CommandUIDefinition>
    <CommandUIDefinition Location="Ribbon.Templates._children">
      <GroupTemplate Id="Ribbon.Templates.Flexible2">
        <Layout Title="LargeLarge" LayoutTitle="LargeLarge">
          <OverflowSection DisplayMode="Large" TemplateAlias="o1" Type="OneRow" />
          <OverflowSection DisplayMode="Large" TemplateAlias="o2" Type="OneRow" />
        </Layout>
        <Layout Title="LargeMedium" LayoutTitle="LargeMedium">
          <OverflowSection DisplayMode="Large" TemplateAlias="o1" Type="OneRow" />
          <OverflowSection DisplayMode="Medium" TemplateAlias="o2" Type="ThreeRow" />
        </Layout>
        <Layout Title="LargeSmall" LayoutTitle="LargeSmall">
          <OverflowSection DisplayMode="Large" TemplateAlias="o1" Type="OneRow" />
          <OverflowSection DisplayMode="Small" TemplateAlias="o2" Type="ThreeRow" />
        </Layout>
        <Layout Title="MediumLarge" LayoutTitle="MediumLarge">
          <OverflowSection DisplayMode="Medium" TemplateAlias="o1" Type="ThreeRow" />
          <OverflowSection DisplayMode="Large" TemplateAlias="o2" Type="OneRow" />
        </Layout>
        <Layout Title="MediumMedium" LayoutTitle="MediumMedium">
          <OverflowSection DisplayMode="Medium" TemplateAlias="o1" Type="ThreeRow" />
          <OverflowSection DisplayMode="Medium" TemplateAlias="o2" Type="ThreeRow" />
        </Layout>
        <Layout Title="MediumSmall" LayoutTitle="MediumSmall">
          <OverflowSection DisplayMode="Medium" TemplateAlias="o1" Type="ThreeRow" />
          <OverflowSection DisplayMode="Small" TemplateAlias="o2" Type="ThreeRow" />
        </Layout>
        <Layout Title="SmallLarge" LayoutTitle="SmallLarge">
          <OverflowSection DisplayMode="Small" TemplateAlias="o1" Type="ThreeRow" />
          <OverflowSection DisplayMode="Large" TemplateAlias="o2" Type="OneRow" />
        </Layout>
        <Layout Title="SmallMedium" LayoutTitle="SmallMedium">
          <OverflowSection DisplayMode="Small" TemplateAlias="o1" Type="ThreeRow" />
          <OverflowSection DisplayMode="Medium" TemplateAlias="o2" Type="ThreeRow" />
        </Layout>
        <Layout Title="SmallSmall" LayoutTitle="SmallSmall">
          <OverflowSection DisplayMode="Small" TemplateAlias="o1" Type="ThreeRow" />
          <OverflowSection DisplayMode="Small" TemplateAlias="o2" Type="ThreeRow" />
        </Layout>
      </GroupTemplate>
    </CommandUIDefinition>
  </CommandUIDefinitions>
  <CommandUIHandlers>
    <CommandUIHandler Command="My Custom Group in Existing Tab.Command" CommandAction="javascript:return true;" />
    <CommandUIHandler Command="My Custom Button.Command" CommandAction="javascript:alert('My custom Action')" />
    <CommandUIHandler Command="My Custom Button 1.Command" CommandAction="javascript:(alert('Teste'));" />
  </CommandUIHandlers>
</CommandUIExtension>

The final output of the Ribbon should be:

Add FlyoutAnchor in Custom Tab/Custom Group

This example add a Custom Large FlyoutAnchor Control in a Custom Tab and Custom Group.

<CommandUIExtension xmlns="http://schemas.microsoft.com/sharepoint/">
  <CommandUIDefinitions>
    <CommandUIDefinition Location="Ribbon.Tabs._children">
      <Tab Id="My Custom Tab" Title="My Custom Tab">
        <Scaling Id="My Custom Tab.Scaling">
          <MaxSize Id="My Custom Group.Scaling.MaxSize" GroupId="My Custom Group" Size="LargeMedium" />
          <Scale Id="My Custom Group.Scaling.Scale" GroupId="My Custom Group" Size="LargeMedium" />
        </Scaling>
        <Groups Id="My Custom Tab.Groups">
          <Group Id="My Custom Group" Title="My Custom Group" Template="Ribbon.Templates.Flexible2" Command="My Custom Group.Command">
            <Controls Id="My Custom Group.Controls">
              <FlyoutAnchor Id="My Custom FlyoutAnchor" LabelText="My Custom FlyoutAnchor" Image16by16="/_layouts/15/1033/images/formatmap16x16.png?rev=33" Image16by16Left="-123" Image16by16Top="-3" Image32by32="/_layouts/15/1033/images/formatmap32x32.png?rev=33" Image32by32Left="-273" Image32by32Top="-173" Command="My Custom FlyoutAnchor.Command" TemplateAlias="o1">
                <Menu Id="My Custom FlyoutAnchor.Menu">
                  <MenuSection Id="My Custom FlyoutAnchor Menu" DisplayMode="Menu32">
                    <Controls Id="My Custom FlyoutAnchor Menu.Controls">
                      <Button Id="My Custom FlyoutAnchor Button" LabelText="My Custom FlyoutAnchor Button" Image16by16="/_layouts/15/1033/images/formatmap16x16.png?rev=33" Image16by16Left="-156" Image16by16Top="-3" Image32by32="/_layouts/15/1033/images/formatmap32x32.png?rev=33" Image32by32Left="-202" Image32by32Top="-137" Command="My Custom FlyoutAnchor Button.Command" TemplateAlias="o1" />
                    </Controls>
                  </MenuSection>
                </Menu>
              </FlyoutAnchor>
            </Controls>
          </Group>
        </Groups>
      </Tab>
    </CommandUIDefinition>
    <CommandUIDefinition Location="Ribbon.Templates._children">
      <GroupTemplate Id="Ribbon.Templates.Flexible2">
        <Layout Title="LargeLarge" LayoutTitle="LargeLarge">
          <OverflowSection DisplayMode="Large" TemplateAlias="o1" Type="OneRow" />
          <OverflowSection DisplayMode="Large" TemplateAlias="o2" Type="OneRow" />
        </Layout>
        <Layout Title="LargeMedium" LayoutTitle="LargeMedium">
          <OverflowSection DisplayMode="Large" TemplateAlias="o1" Type="OneRow" />
          <OverflowSection DisplayMode="Medium" TemplateAlias="o2" Type="ThreeRow" />
        </Layout>
        <Layout Title="LargeSmall" LayoutTitle="LargeSmall">
          <OverflowSection DisplayMode="Large" TemplateAlias="o1" Type="OneRow" />
          <OverflowSection DisplayMode="Small" TemplateAlias="o2" Type="ThreeRow" />
        </Layout>
        <Layout Title="MediumLarge" LayoutTitle="MediumLarge">
          <OverflowSection DisplayMode="Medium" TemplateAlias="o1" Type="ThreeRow" />
          <OverflowSection DisplayMode="Large" TemplateAlias="o2" Type="OneRow" />
        </Layout>
        <Layout Title="MediumMedium" LayoutTitle="MediumMedium">
          <OverflowSection DisplayMode="Medium" TemplateAlias="o1" Type="ThreeRow" />
          <OverflowSection DisplayMode="Medium" TemplateAlias="o2" Type="ThreeRow" />
        </Layout>
        <Layout Title="MediumSmall" LayoutTitle="MediumSmall">
          <OverflowSection DisplayMode="Medium" TemplateAlias="o1" Type="ThreeRow" />
          <OverflowSection DisplayMode="Small" TemplateAlias="o2" Type="ThreeRow" />
        </Layout>
        <Layout Title="SmallLarge" LayoutTitle="SmallLarge">
          <OverflowSection DisplayMode="Small" TemplateAlias="o1" Type="ThreeRow" />
          <OverflowSection DisplayMode="Large" TemplateAlias="o2" Type="OneRow" />
        </Layout>
        <Layout Title="SmallMedium" LayoutTitle="SmallMedium">
          <OverflowSection DisplayMode="Small" TemplateAlias="o1" Type="ThreeRow" />
          <OverflowSection DisplayMode="Medium" TemplateAlias="o2" Type="ThreeRow" />
        </Layout>
        <Layout Title="SmallSmall" LayoutTitle="SmallSmall">
          <OverflowSection DisplayMode="Small" TemplateAlias="o1" Type="ThreeRow" />
          <OverflowSection DisplayMode="Small" TemplateAlias="o2" Type="ThreeRow" />
        </Layout>
      </GroupTemplate>
    </CommandUIDefinition>
  </CommandUIDefinitions>
  <CommandUIHandlers>
    <CommandUIHandler Command="My Custom Group.Command" CommandAction="javascript:return true;" />
    <CommandUIHandler Command="My Custom FlyoutAnchor.Command" CommandAction="" />
    <CommandUIHandler Command="My Custom FlyoutAnchor Button.Command" CommandAction="javascript:alert('My Custom Action');" />
  </CommandUIHandlers>
</CommandUIExtension> 
The final output of the Ribbon should be:



With the ProcessLynx SharePoint App, you be able to see the Ribbon Structure of the FlyoutAnchor in the Custom Tab.

This example came from processlynx SharePoint app (Ribbon)

Add Button Control in Existing Tab/Group

<CommandUIExtension xmlns="http://schemas.microsoft.com/sharepoint/">
  <CommandUIDefinitions>
    <CommandUIDefinition Location="Ribbon.Library.Actions.Controls._children">
      <Button Id="My Button - existing location" LabelText="Existing location" Image16by16="/_layouts/15/1033/images/formatmap16x16.png?rev=33" Image16by16Left="-266" Image16by16Top="-1" Image32by32="/_layouts/15/1033/images/formatmap32x32.png?rev=33" Image32by32Left="-509" Image32by32Top="-0" Command="My Button - existing location.Command" TemplateAlias="o1" />
    </CommandUIDefinition>
  </CommandUIDefinitions>
  <CommandUIHandlers>
    <CommandUIHandler Command="My Button - existing location.Command" CommandAction="javascript:alert('My Button in existing location');" />
  </CommandUIHandlers>
</CommandUIExtension>

The final output of the Ribbon should be:

Override Control

This option can be used to re-write and change the behavior of existing Out of the box Ribbon Control.

Here are the Default Server Ribbon Customization Locations you can use to change the behavior.
http://msdn.microsoft.com/en-us/library/office/ee537543(v=office.14).aspx

Hide existing Ribbon control

The following example show how you can use XML UI Extension to hide existing controls like Open with Explorer "Ribbon.Library.Actions.OpenWithExplorer"


<CommandUIExtension xmlns="http://schemas.microsoft.com/sharepoint/">
  <CommandUIDefinitions>
    <CommandUIDefinition Location="Ribbon.Library.Actions.OpenWithExplorer">
    </CommandUIDefinition>
  </CommandUIDefinitions>
</CommandUIExtension>

The final output of the Ribbon should be without the option:

Replace existing Ribbon control

This example, show how we can replace the existing "Open with Explorer" with custom a Custom Action and image associated.

Here the example of the XML code:

<CommandUIExtension xmlns="http://schemas.microsoft.com/sharepoint/">
  <CommandUIDefinitions>
    <CommandUIDefinition Location="Ribbon.Library.Actions.OpenWithExplorer">
      <Button Id="New Action" LabelText="New Action" Command="New Action.Command" TemplateAlias="o2" />
    </CommandUIDefinition>
  </CommandUIDefinitions>
  <CommandUIHandlers>
    <CommandUIHandler Command="New Action.Command" CommandAction="javascript:alert('New Action');" />
  </CommandUIHandlers>
</CommandUIExtension>
The final output of the Ribbon should be:

How to get Images position using SharePoint out of the Box image 


When we are creating custom Buttons in the Ribbon it is possible to select a image position base in 16x16 or 32x32, only need to know the coordinates where is the icon as show in the XML and image bellow.

<Button Id="My Button - existing location" LabelText="Existing location" Image16by16="/_layouts/15/1033/images/formatmap16x16.png?rev=33" Image16by16Left="-266" Image16by16Top="-1" Image32by32="/_layouts/15/1033/images/formatmap32x32.png?rev=33" Image32by32Left="-509" Image32by32Top="-0" Command="My Button - existing location.Command" TemplateAlias="o1" />

Image position base in the image url:
Select the 16x16 position "shown in pink"
Image16by16Left="-266
http://[Site]/_layouts/15/1033/images/formatmap16x16.png?rev=33

This example came from processlynx SharePoint app (Ribbon)
Select the 32x32 position "shown in pink"
Image32by32Left="-509"
http://[Site]/_layouts/15/1033/images/formatmap32x32.png?rev=33

This example came from processlynx SharePoint app (Ribbon)

PS: If you are using SharePoint Online be careful, Microsoft is changing the position of the icons.

All this examples where made in 10 minutes using a very user friendly "no knowledge of XML is needed" SharePoint App created by Processlynx, for more info you can send email.
Hope you like this article,
Kind regards,
Andre Lage