We wanted to add a button to the ribbon which lets the user select two or more items, and do simultaneous edits to selected columns.
Creating ribbon buttons is eeeeeasy.
Cksdev even has a built-in template for it.
What Cksdev doesn't solve is finding the correct location.
This can be a frustrating job.
Especially if you only do it from a SharePoint solution.
I chose to do it from a tiny console application, which cut the develop-test-swear-cycle to seconds rather than minutes.
It is perhaps worth sharing.
static void Main(string[] args)
{
var rb = new RibbonAdder();
var listname = "Shared Documents";
rb.AddButton(listname);
rb.ListActions(listname);
System.Console.WriteLine("Button added. Test, then press the any key.");
System.Console.ReadKey();
rb.RemoveAllTheCustomActions(listname);
}
I add the button, list the custom actions for debugging purposes, wait a bit, and remove it again.
public void AddButton(string listname)
{
using (var site = new SPSite("http://Norgeous:100/"))
{
var web = site.OpenWeb();
var list = web.Lists.TryGetList(listname);
if (list == null)
return;
var action = list.UserCustomActions.Add();
action.Location = "CommandUI.Ribbon";
action.Sequence = 45;
action.Title = "Test Action";
action.CommandUIExtension = @"<commanduiextension>
<commanduidefinitions>
<commanduidefinition location="Ribbon.Documents.EditCheckout.Controls._children">
<button Id='Ribbon.DocumentManagement.EditLibraryItems.Button'
Command='Ribbon.DocumentManagement.EditLibraryItems.Command'
Sequence='45'
Image16by16='/_layouts/images/Norgeous/EditItems16.png'
Image32by32='/_layouts/images/Norgeous/EditItems32.png'
Description='Edit library items'
LabelText='Edit items'
TemplateAlias='o2' />
<commanduihandlers>
<commanduihandler command="Ribbon.DocumentManagement.EditLibraryItems.Command" commandaction="" javascript:=""
javascript:
var selectedItems = SP.ListOperation.Selection.getSelectedItems();
var selectedIds = '';
var listId = SP.ListOperation.Selection.getSelectedList();
var i = 0;
while(i!=selectedItems.length)
{
selectedIds += selectedItems[i].id + ',';
i++;
}
selectedIds = escape(selectedIds.substring(0,selectedIds.length-1));
var siteCollUrl = '{SiteUrl}';
var options = SP.UI.$create_DialogOptions();
options.title = 'Edit items';
options.width = 850;
options.height = 600;
options.allowClose = true;
options.url = siteCollUrl + '/_layouts/Norgeous/editmultipleitems.aspx?ids='+selectedIds+'&listId='+listId;
SP.UI.ModalDialog.showModalDialog(options);""
EnabledScript='javascript:SP.ListOperation.Selection.getSelectedItems().length > 1;' />
</commanduihandler>
";
action.Update();
}
}
</button></commanduidefinition></commanduidefinitions></commanduiextension>
'EnabledScript' only enables the button if at least two items have been selected. If one item is selected, the built in "edit item" is a better choice.
The last two methods are embarrassingly simple:
public void ListActions(string listname)
{
using (var site = new SPSite("http://Norgeous:100/"))
{
var web = site.OpenWeb();
var list = web.Lists.TryGetList(listname);
foreach (SPUserCustomAction customAction in list.UserCustomActions)
{
System.Console.WriteLine(customAction.Title);
}
}
}
public void RemoveAllTheCustomActions(string listname)
{
using (var site = new SPSite("http://Norgeous:100/"))
{
var web = site.OpenWeb();
var list = web.Lists.TryGetList(listname);
list.UserCustomActions.Clear();
}
}
Note: I've tried to tidy up the mess left by the Geekswithblogs editor. But I doubt I found all the errors that were introduced. The xml should be PascalCased. Read the javascript carefully, and leave a note if you find anything.