Overriding Save Button Behavior in SharePoint

15 12 2015

Overriding Save Button Behavior in SharePoint

There are multiple ways to hookup a customized behavior with SharePoint “SaveButton”. Below are two options that can make your day.  

Option 1

Inherit from ListFieldIterator class and hookup OnSaveHandler with your custom method. Below is the simple code to customize SaveButton behavior while utilizing ListFieldIterator class.

public class CustomListFieldIterator : ListFieldIterator
{
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
SPContext.Current.FormContext.OnSaveHandler += new EventHandler(CustomOnSave);
}
protected void CustomOnSave(object sender, EventArgs e)
{
//custom code
// returns true if item saved else false
Boolean returnValue = SaveButton.SaveItem(SPContext.Current, false, "comments if any");
//custom code
}
}

Note:

For this approach static method SaveButton.SaveItem with parameters is available. Make sure to pass true for DocumentLibrary list and false for the normal list. It won’t have any issues saving the items but some stuff like “Check Out \ Check In” might not work as expected.

Option 2

Inherit from SaveButton class and override the virtual method SaveItem from base class.

public partial class CustomSaveButton : SaveButton
{
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
}
protected override bool SaveItem()
{
//custom code
bool result = base.SaveItem();
//custom code
return result;
}
}