Fetching selected list items in SharePoint 2010 is very simple. To achieve such a functionality You can use Custom Ribbon Button. In this example I will use the code from my previous post on creating custom ribbon button.

The code that let’s You fetch selected list items looks like this:


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;
 }

When You have the selected items id’s, then You can for example pass them to the new page by opening new modal dialog with prepared query string:


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: '{SiteUrl}/_layouts/MyPage.aspx?Items=' + myItems + '&ListId={SelectedListId}&Source={Source}',
 tite: 'My Page',
 allowMaximize: false,
 showClose: true,
 width: 500,
 height: 160,
 dialogReturnValueCallback: resultCallback };

 SP.UI.ModalDialog.showModalDialog(options);
 }

Full elements.xml looks like this:


<?xml version="1.0" encoding="utf-8" ?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">

<CustomAction

Id="CAAlertRibbonButton"
RegistrationId="100"
RegistrationType="List"
Location="CommandUI.Ribbon"
Sequence="5"
Title="Alert Ribbon Button">

<CommandUIExtension>
<CommandUIDefinitions>
<CommandUIDefinition Location="Ribbon.List.Settings.Controls._children">
<Button
Id="AlertRibbonButtonId"
Alt="Alert Ribbon Button"
Sequence="5"
Command="CMDAlertRibbonButton"
Image32by32="/_layouts/images/menulistsettings.gif"
Image16by16="/_layouts/images/itdcl.gif"
LabelText="Alert Ribbon Button"
TemplateAlias="o1" />
</CommandUIDefinition>
</CommandUIDefinitions>

<CommandUIHandlers>
<CommandUIHandler
Command="CMDAlertRibbonButton"
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: '{SiteUrl}/_layouts/MyPage.aspx?Items=' + myItems + '&amp;ListId={SelectedListId}&amp;Source={Source}',
 tite: 'My Page',
 allowMaximize: false,
 showClose: true,
 width: 500,
 height: 160,
 dialogReturnValueCallback: resultCallback };

SP.UI.ModalDialog.showModalDialog(options);
 }" />
</CommandUIHandlers>

</CommandUIExtension>
</CustomAction>
</Elements>

To retrieve the id’s in the page opened using modal dialog use below code:


string[] items = Request["Items"] != null ? Request["Items"].Split(new string[] { "|" }, StringSplitOptions.RemoveEmptyEntries) : new string[] { };//get id's

SPList list = null;

SPSecurity.RunWithElevatedPrivileges(() =>
 {
 using (SPSite site = new SPSite(sourceUrl))
 {
 using (SPWeb web = site.OpenWeb())
 {
 list = web.Lists[new Guid(Request["ListId"])];//get list reference in elevated mode
 }
 }
 });

foreach (string id in Items)
 {
 var listItem = list.GetItemById(Int32.Parse(id));//Fetch list item

//DO SOME ACTION

}