<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<CustomAction
Id="ShowHelloWorldButton"
RegistrationType="List"
RegistrationId="100"
Location="CommandUI.Ribbon.ListView"
Rights="EditListItems">
<CommandUIExtension>
<CommandUIDefinitions>
<CommandUIDefinition
Location="Ribbon.ListItem.Actions.Controls._children">
<Button
Id="Ribbon.ListItem.Actions.Controls.ShowHelloWorldButton"
Alt="Show HelloWorld Alert"
Sequence="1"
Image32by32="/_layouts/DemoSharePointRibbon/HelloWorld.jpg"
Command="ShowHelloWorldCommand"
LabelText="Hello World"
TemplateAlias="o1"
CommandType="General"/>
</CommandUIDefinition>
</CommandUIDefinitions>
<CommandUIHandlers>
<CommandUIHandler
CommandAction="javascript:alert('Hello World');" />
</CommandUIHandlers>
</CommandUIExtension>
</CustomAction>
</Elements>
Property
Description
RegistrationType
None, List, ContentType, ProgId or FileType
E.g. To show ribbon button on SharePoint list set List
RegistrationId
It is identifier of the list or item content type that this action is associated with. E.g. 100 RegistrationId is used to attach action with the custom list.
Location
Location of this custom action. Few important locations are mentioned below:
· Microsoft.SharePoint.SiteSettings
· If the custom action is a menu item or toolbar button, the possible options include EditControlBlock, NewFormToolbar, DisplayFormToolbar, and EditFormToolbar.
· If the CustomAction element contains a CommandUIExtension child element, the Location must start with "CommandUI.Ribbon". E.g. In the "Hello World" sample we used CommandUI.Ribbon.ListView.
Rights
· If it is not specified, the action always appears in the list of actions.
· Specifies a set of rights, so that it will visible only if user has all specified rights (logical AND will be applied if we assign multiple permission comma separated).
Few important values for "Rights" property are mentioned below:
ViewListItems, AddListItems, EditListItems, OpenItems, ViewVersions, ManagePersonalViews, ViewFormPages, Open, ViewPages, CreateSSCSite, BrowseDirectories, BrowseUserInfo, AddDelPrivateWebParts, UpdatePersonalWebParts, UseRemoteAPIs, CreateAlerts, EditMyUserInfo
E.g. Specified "EditListItems" right to visible this button to the user who has edit item permission.
Sequence
Specifies the ordering priority for actions
Order of placement if there are multiple ribbon buttons
Command
The name of the command to execute when the control is clicked.
Note: Command attribute of the control should be the same as the value of the Command attribute of a corresponding CommandUIHandler element.
E.g. "ShowHelloWorldCommand" is used in our sample.
CommandType
General | OptionSelect | IgnoredByMenu
a. Add this mentioned below custom action in your element file and put your JavaScript in the JavaScript file (e.g. HelloWorld.js).
Id="ShowHelloWorldCommand.Script"
Location="ScriptLink"
ScriptSrc ="/_layouts/DemoSharePointRibbon/js/HelloWorld.js"/>
b. Call JavaScript method from "CommandAction" instead of writing inline JavaScript.
//Using JavaScript client object model, we can get the selected items id
function GetSelectedItemsId() {
var ctx = SP.ClientContext.get_current();
var items = SP.ListOperation.Selection.getSelectedItems(ctx);
var selectedItems = '';
for (index in items) {
selectedItems = selectedItems + ',' + items[index].id;
}
return selectedItems;
a. Add following attribute in the element file under "CommandUIHandler" section:
Command="ExternalFileShowHelloWorldCommand"
EnabledScript="javascript:EnableButton();"
CommandAction="javascript:ShowAlert();" />
b. Add following JavaScript code in the external file:
function EnableButton() {
return items.length > 0;
a. Put following code for postback and server side using the request id we can identify the button, and then perform action accordingly:
CommandAction="__doPostBack('ShowHelloWorldPostback','');" />
//On page load check for request id
if (this.Page.Request["__EVENTTARGET"] == "ShowHelloWorldPostback")
{
//Call your method
b. Use JavaScript client side object model to perform action like add, edit, delete.
For example, suppose we have to provide functionality to change status of selected items to approve. So we can use following client side code to achieve the same:
function ApproveSelectedItems() {
var web = ctx.get_web();
var crList = web.get_lists().getById (SP.ListOperation.Selection.getSelectedList(ctx));
var index;
var selectedItem = crList.getItemById(items[index].id);
selectedItem.set_item(columnName, columnValue);
if ('' != currentList && changeRequestListName == currentList) {
selectedItem.set_item('Status', 'Approve');
selectedItem.update();
ctx.executeQueryAsync(Function.createDelegate(this, this.onsccess),
Function.createDelegate(this, this.onFail));
function onsccess (sender, args) {
if (this.SuccessMsg != undefined || this.SuccessMsg != null) {
alert(this.SuccessMsg);
function onfailure (sender, args) {
alert(MsgOperationFailed);
c. Using layout page we can provide user to input information before updating or performing any action on any item. Follow mention below steps to achieve the same:
i. Create a layout page say approve.aspx
ii. Write your server side code in the code behind of layout page
iii. When user click on ribbon button, open layout page in the popup using SharePoint modal dialog API
iv. Pass selected items id as a query string parameter to the layout page
v. Read query string parameter
vi. Perform whatever action you want to do it on selected items