Search This Blog

Saturday, March 21, 2015

SharePoint 2010 : Insert custom icon in a custom group on Ribbon of Document Library.

Goal

Insert a custom icon(Email icon) on the SharePoint Document Library. The Icon should show in a Custom Group.

Challange

I thought getting the Icon on the top was easy(actually it was easy to place an icon on the top). But to place the 
icon in a Custom Group was a bit tough that it seems.

Follow the steps to achieve

  1. Open Visual Studio 2010, Create an empty SharePoint Application.
  2. Add a Module, Edit the Element.xml
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<CustomAction
Id="EmailDocsCustom"
Location="CommandUI.Ribbon"
RegistrationType="ContentType"
RegistrationId="0x01">
<CommandUIExtension>
<CommandUIDefinitions>
<CommandUIDefinition Location="Ribbon.Documents.Groups._children">
<Group
Id="EmailDocsCustomGroup"
Sequence="100"
Description="E-Mail Controls"
Title="E-Mail Controls"
Template="EmailDocsCustomGroupTemplate">
<Controls Id="EmailDocsCustomGroupControl">
<Button
Id="EmailDocsCustomGroupControlButton"
Sequence="5"
Command="SimpleAlert"
Image32by32="/_layouts/1033/images/formatmap32x32.png" Image32by32Left="-448"
Image32by32Top="-128"
ToolTipTitle="Email Documents"
ToolTipDescription="Select the documents which you want to email!"
LabelText="Email Selected Documents"
TemplateAlias="o1" />
</Controls>
</Group>
</CommandUIDefinition>
<CommandUIDefinition Location="Ribbon.Templates._children">
<GroupTemplate Id="EmailDocsCustomGroupTemplate">
<Layout Title="LargeLarge">
<OverflowSection Type="OneRow" TemplateAlias="o1" DisplayMode="Large"/>
<OverflowSection Type="OneRow" TemplateAlias="o2" DisplayMode="Large"/>
</Layout>
</GroupTemplate>
</CommandUIDefinition>
<CommandUIDefinition Location="Ribbon.Documents.Scaling._children">
<MaxSize Id="Ribbon.Documents.Scaling.Custom.MaxSize" Sequence="15"
GroupId="EmailDocsCustomGroup" Size="LargeLarge" />
</CommandUIDefinition>
</CommandUIDefinitions>
<CommandUIHandlers>
<CommandUIHandler Command="SimpleAlert" CommandAction="javascript:OpenEmailPage();" />
</CommandUIHandlers>
</CommandUIExtension>
</CustomAction>

<CustomAction
Id="EmailRelatedScript"
Location="ScriptLink"
ScriptSrc ="/_layouts/WhatEvePathYouHave/EmailScript.js"/>
</Elements>

Place the EmailScript.js at C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\WhatEvePathYouHave(You can map the folder and do this).
 With the help of this script you can do manipulations on the click of the icon placed on the top ribbon.
 Below is the script in EmailScript.js.
//Get the current user and add the values found in the Email settings list
function OpenEmailPage() {

    //Gets the current Context
    var _ctx = new SP.ClientContext.get_current();
    //Selected Items Variables
    var _itemIds = "";
    //Get current list id
    var _listId = SP.ListOperation.Selection.getSelectedList();
    var _listUrl = window.location.href;
    //get all selected list items
    var _selectedItems = SP.ListOperation.Selection.getSelectedItems(_ctx);
    //collect selected item ids
    for (var i = 0; i < _selectedItems.length; i++) {
        if (_itemIds == "")
        { _itemIds += _selectedItems[i].id; }
        else
        { _itemIds += "," + _selectedItems[i].id; }
    }

    alert('ListID = ' + _listId + "\nListUrl = " + _listUrl + "\nIDs=" + _itemIds);    
}
Before deploying the solution, check a document library. The ribbon on the top in the documents should like 
the below given image.

Deploy the solution. Visit a Document Library, select docs and the new Icon should show up on the top
 right corner.
I am not very good with Ribbons and spent a lot of time to get just this portion. So thought if this can help
many other who dont have time and have to get this quickly.

No comments:

Post a Comment