Scenario: Enable bulk approval/rejection of list items in MOSS 2007 lists.
Now edit the Allitems.aspx page of the list. Add a content editor webpart and the following script in the CEWP.
Solution: The solution is based on javascript by Wesley Bakker. This script creates check boxes in each row of the list view webpart and has a helper function which provides the list of item Ids selected. This script does not work properly if the list view is modified to use Grouping. The solution for this is given by Brian Frisch here.
I am using SPServices JQuery library for actual bulk approval/rejection. Upload the following javascript file to document library.
Name the file as ListItemSelection.jsfunction CreateParentInputCheckBox(webPartId) { return $("").append( $("<input title="(de)select all items" type="checkbox" />").attr("id", webPartId + "0") .click(function() { var checked = $(this).attr("checked"); $("[id^=" + webPartId + "_]").attr("checked", checked); }) ); function CreateChildInputCheckBox(webPartId, itemId) { return $("").append( $("<input type="checkbox" />").attr("id", webPartId + "_" + itemId) .val(itemId) .click(function() { $("#" + webPartId + "0").attr("checked", $(this).attr("checked") && $("[id^=" + webPartId + "_]:not(:checked)").length == 0); }) ); } function AddCheckBoxesToListView(webPartId) { $(" table.ms-listviewtable>tbody") .find(">tr.ms-viewheadertr").prepend(CreateParentInputCheckBox(webPartId)).end() .find(">tr:not(.ms-viewheadertr)") .each(function() { var itemId = $(this).find("td.ms-vb-title>table[id]").attr("id"); if (itemId) { $(this).prepend(CreateChildInputCheckBox(webPartId, itemId)); } }); } function IsSelectable(webPartId) { var selectableItems = $("#" + webPartId + " table.ms-listviewtable>tbody>tr:not(.ms-viewheadertr)>td.ms-vb-title>table[id]").length; return selectableItems > 0; } function RemoveCheckBoxesFromListView(webPartId) { $("[id^=" + webPartId + "_], #" + webPartId + "0").parent().remove(); } function GetSelectedItemsString(webPartId) { var selectedIds = new Array(); $("[id^=" + webPartId + "_]:checked") .each(function() { selectedIds.push($(this).val()); }); return selectedIds.join(","); } function GetSelectedItemsArray(webPartId){ var selectedIds = new Array(); $("[id^=" + webPartId + "_]:checked") .each(function() { selectedIds.push($(this).val()); }); return selectedIds; } var existingMethod = ExpGroupReceiveData; ExpGroupReceiveData = function(){ existingMethod.apply(this , arguments); // apply the original properties of the function if (!wpID){ // let's identify the webpart in question wpID = $('tbody#titl'+arguments[1]).parents('div[id^=WebPart]').attr('id'); } var selectedItems = GetSelectedItemsArray(wpID); // let's save state of selected checkboxes RemoveCheckBoxesFromListView(wpID); // remove existing checkboxes AddCheckBoxesToListView(wpID); // add checkboxes to webpart group items $.each(selectedItems, function(i){ // reapply state to previously selected checkboxes $('#' + wpID + '_' + selectedItems[i]).attr("checked", true); }); }
Now edit the Allitems.aspx page of the list. Add a content editor webpart and the following script in the CEWP.
<script type="text/javascript" language="javascript" src="/Shared%20Documents/JS/jquery.min.js"></script>
<script type="text/javascript" language="javascript" src="/Shared%20Documents/JS/jquery.SPServices-0.6.2.min.js"></script>
<script type="text/javascript" language="javascript" src="/Shared%20Documents/JS/ListItemSelection.js"></script>
<script language="javascript" type="text/javascript">
var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
var string_length = 8;
var randomstring = '';
for (var i=0; i<string_length; i++) {
var rnum = Math.floor(Math.random() * chars.length);
randomstring += chars.substring(rnum,rnum+1);
}
var wpID = randomstring;
_spBodyOnLoadFunctionNames.push("AddCheckBoxesToListView(wpID)");
function Approve()
{
if (ctx.isModerated == true)
{
var selectedItems = GetSelectedItemsString(wpID);
if (selectedItems == "")
{
alert('Please select items.');
return;
}
var items = selectedItems.split(',');
var updateCommand = "<Batch>";
for (i = 0; i<items.length; i++)
{
updateCommand += "<Method ID='"+items[i]+"' Cmd='Moderate'>" +
"<Field Name='ID'>"+items[i]+"</Field>" +
"<Field Name='_ModerationStatus' Type='ModStat'>0</Field>" +
"</Method>"
}
updateCommand += "</Batch>";
$().SPServices({
operation: "UpdateListItems",
listName: ctx.ListTitle ,
updates: updateCommand,
completefunc: function (xData, Status) {
alert("Item(s) approved successfully.");
window.location.href=window.location.href;
}
});
}
else
{
alert('Please turn on content approval on the list.');
}
}
function Reject()
{
if (ctx.isModerated == true)
{
var selectedItems = GetSelectedItemsString(wpID);
if (selectedItems == "")
{
alert('Please select items.');
return;
}
var items = selectedItems.split(',');
var updateCommand = "<Batch>";
for (i = 0; i<items.length; i++)
{
updateCommand += "<Method ID='"+items[i]+"' Cmd='Moderate'>" +
"<Field Name='ID'>"+items[i]+"</Field>" +
"<Field Name='_ModerationStatus' Type='ModStat'>1</Field>" +
"</Method>"
}
updateCommand += "</Batch>";
$().SPServices({
operation: "UpdateListItems",
listName: ctx.ListTitle,
updates: updateCommand,
completefunc: function (xData, Status) {
alert("Item(s) rejected successfully.");
window.location.href=window.location.href;
}
});
}
else
{
alert('Please turn on content approval on the list.');
}
}
</script>
<a href="#" onClick="Approve();return false">Bulk Approve</a>
<a href="#" onClick="Reject();return false">Bulk Reject</a>
Notice, that the script requires JQuery and SPServices.