Home > sharepoint, webpart > Approve all items button in a SharePoint View

Approve all items button in a SharePoint View

Situation: You’ve been tasked with building a mechanism for an approver to approve all the pending items in a SharePoint list. 

Implementation: Single “Approve All” button WebPart that only shows for approvers. This WebPart can be shown on one of the view pages of the list. 

Concepts:

  1. Place the WebPart on a View of the List
    • Go to the list
    • Click Edit Page
    • Add the WebPart in the top zone
  2. Loop through the items in the current list and set the approve flag
    • This is in the bgnApprove_click event handler below
    • This checks if the item is in pending status and approves the item
    • This will cancel approval workflows currently running on the item
 
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Workflow; 

namespace ApproveAll
{
[Guid("a73bd0c2-a842-4c37-8021-447f550428b5")]
public class ApproveAll : Microsoft.SharePoint.WebPartPages.WebPart
{
private bool _error = false; 

public ApproveAll()
{
this.ExportMode = WebPartExportMode.All;
} 

///
/// Create all your controls here for rendering.
/// Try to avoid using the RenderWebPart() method.
///
protected override void CreateChildControls()
{
if (!_error)
{
try
{
base.CreateChildControls(); 

//get the list
SPList list = SPContext.Current.List; 

//Show the approve button for users with approve permissions to this list
if (list.DoesUserHavePermissions(SPBasePermissions.ApproveItems))
{
Button btnApprove = new Button();
btnApprove.Text = "Approve All";
btnApprove.CssClass = "ApproveAllButton";
btnApprove.Click += new EventHandler(btnApprove_Click);
this.Controls.Add(btnApprove);
}
}
catch (Exception ex)
{
HandleException(ex);
}
}
} 

///
/// Loops through all items in the current list and approves them.
///
///
///
void btnApprove_Click(object sender, EventArgs e)
{
//get the list
SPList list = SPContext.Current.List; 

//approve all items
foreach (SPListItem item in list.Items)
{
if (item.Level == SPFileLevel.Published)
continue; 

SPFile sourceFile = item.File;
if (sourceFile != null)
{
//Update files
if (sourceFile.Item.ModerationInformation.Status == SPModerationStatusType.Pending)
{
CancelWorkflows(list, item);
sourceFile.Approve("Bulk Approval by" + SPContext.Current.Web.CurrentUser.LoginName);
}
}
else
{
//Update simple list items
if (item.ModerationInformation.Status == SPModerationStatusType.Pending)
{
CancelWorkflows(list, item);
item.ModerationInformation.Status = SPModerationStatusType.Approved;
item.ModerationInformation.Comment = "Bulk Approval by" + SPContext.Current.Web.CurrentUser.LoginName;
item.Update();
}
}
}
} 

private static void CancelWorkflows(SPList list, SPListItem item)
{
if (list.DefaultContentApprovalWorkflowId != Guid.Empty)
{
SPSecurity.RunWithElevatedPrivileges(delegate{
foreach (SPWorkflow workflow in item.Workflows)
{
if (workflow.ParentAssociation.Id == list.DefaultContentApprovalWorkflowId)
SPWorkflowManager.CancelWorkflow(workflow);
}
});
}
} 

///
/// Ensures that the CreateChildControls() is called before events.
/// Use CreateChildControls() to create your controls.
///
///
protected override void OnLoad(EventArgs e)
{
if (!_error)
{
try
{
base.OnLoad(e);
this.EnsureChildControls();
}
catch (Exception ex)
{
HandleException(ex);
}
}
} 

///
/// Clear all child controls and add an error message for display.
///
///
private void HandleException(Exception ex)
{
this._error = true;
this.Controls.Clear();
this.Controls.Add(new LiteralControl(ex.Message));
}
}
} 

Sorry for the lack of proper indentation in the code. The blog did it 🙂

Now, build and deploy the custom WebPart. Then, go to any view in your List. Edit the page and drop the custom Approve All WebPart in the WebZone (right above the list view). 

*Just remember: this only works if you put the WebPart on a view “in the list” because it infers the list object from the current context. Also, if you want this on multiple views for the list, you must place it on all the views. 

Happy hacking!

Advertisements
Categories: sharepoint, webpart

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: