SharePoint Approve multiple items in document library

Recently I had a requirement of approving multiple items in the Document library. Since this had to be done in SharePoint 2010, I thought of using client side object model and here is how I did it.

Create an empty SharePoint project and name it MultipleApproval as shown:

Click on the solution and Add New Item. Select Empty Element in SharePoint templates.


Open Elements.xml file and add following code.

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
    <CustomAction Id="Ribbon.Documents.Workflows.BulkApproveButton"
    RegistrationId="101"
    Location="CommandUI.Ribbon"
    RegistrationType="List">
    <CommandUIExtension>
    <CommandUIDefinitions>
    <CommandUIDefinition Location="Ribbon.Documents.Workflow.Controls._children">
    <Button Id="Ribbon.Documents.Workflow.Approve"
    Alt="Approve multiple items"
    Command="ApproveMultipleItems"
    LabelText="Approve All"
    Image16by16="/_layouts/images/placeholder32x32.png"
    Image32by32="/_layouts/images/placeholder32x32.png"
    TemplateAlias="o1"/>
    </CommandUIDefinition>
    </CommandUIDefinitions>
    <CommandUIHandlers>
    <CommandUIHandler Command="ApproveMultipleItems" CommandAction="javascript:
        var context;
        var web;
        var itemCount;
        var count = 0 ;
        SP.UI.Notify.addNotification('Approving item(s)...');
        approveItems();
        function approveItems ()
        {
            context = SP.ClientContext.get_current();
            web = context.get_web();
            context.load(web);
            var currentLibId = SP.ListOperation.Selection.getSelectedList();
            var currentLib = web.get_lists().getById(currentLibId);
            var selectedItems = SP.ListOperation.Selection.getSelectedItems(context);
            itemCount = CountDictionary(selectedItems);
            for (var i in selectedItems)
            {
                var currentItem = currentLib.getItemById(selectedItems[i].id);
                context.load(currentItem);
                currentItem.set_item('_ModerationStatus', 0);
                currentItem.update();
                context.executeQueryAsync(onQuerySuccess, onQueryFailed);
            }
        }                
        function onQuerySuccess(sender, args)
        {
            count++;
            if (count == itemCount)
            {
                //Refresh the page so that changes can be seen.
                window.location.href = window.location.href;
            }
        }
        function onQueryFailed(sender, args)
        {
            alert(args.get_message());
        }
        " EnabledScript="javascript:
        function enableBulkApproveButton() {
        var context = SP.ClientContext.get_current();
        return SP.ListOperation.Selection.getSelectedItems(context).length >= 1;
        }
        enableBulkApproveButton();">
    </CommandUIHandler>
    </CommandUIHandlers>
    </CommandUIExtension>
    </CustomAction>
</Elements>

Now go to your document library and enable versioning in that. Add few documents to the library.
In the Ribbon select Library Tools--> Documents. Select the documents that need to be approved and click on Approve  All button in the Ribbon.