SharePoint Override save button in list form by using csharp

Well i a custom list form you always have a cancel and a save button. I already posted how to create & deploy custom list forms. But i didn’t mentioned that it is possible to override the save handler. That’s pretty useful if you want to do some extra action after the user clicks on the save button, or if you need to write some data of the form to an external system.

Well my other post are here:

Part 1: Setting up a solution with a list definition, a list instance and deploy it.

Part 2: Creating a custom list form and connect it with the list definition.

Part 3: Necessary settings for custom listform in Visual Studio

Cause i didn’t told you how to overwrite the save button in order to do some custom actions on this part i’ll do it now. It’s really simple. See the code below:

[sourcecode language=”csharp”]

//ASPX PAGE

//In your aspx page a control like this should be available

//CODE BEHIND

//Override Save Button in the init method
protected override void OnInit(EventArgs e)
{

//In the init method you overrite the event handler for saving with your save button action
base.OnInit(e);
SPContext.Current.FormContext.OnSaveHandler += new EventHandler(mySaveHandler);

}

//My Save Button Function
protected void mySaveHandler(object sender, EventArgs e)
{

//you can validate the page if you like
Page.Validate();
if (Page.IsValid)
{
//Do whatever you wanna do
SPListItem currentItem = SPContext.Current.ListItem;


}
}

[/sourcecode]

That’s it. Really easy right?

..:: I LIKE SHAREPOINT ::..

The article or information provided here represents completely my own personal view & thought. It is recommended to test the content or scripts of the site in the lab, before making use in the production environment & use it completely at your own risk. The articles, scripts, suggestions or tricks published on the site are provided AS-IS with no warranties or guarantees and confers no rights.

About Karsten Schneider 312 Articles
Consultant for Microsoft 365 Applications with a strong focus in Teams, SharePoint Online, OneDrive for Business as well as PowerPlatform with PowerApps, Flow and PowerBI. I provide Workshops for Governance & Security in Office 365 and Development of Solutions in the area of Collaboration and Teamwork based on Microsoft 365 and Azure Cloud Solutions. In his free time he tries to collect tipps and worthy experience in this blog.

3 Comments

  1. Hello, This question is pertaining to SharePoint 2010.

    I have one list which has two content types. We have modified the detault forms (new,edit and display) of each content type to its individual custom application pages. We have done this through coding.

    SPContentType CT1= requestsList.ContentTypes[“CT1”];

    //Change Edit and edit URL to custom application page

    CT1.NewFormUrl = “_layouts/folder/customnewapplicationpage.aspx;

    CT1.EditFormUrl = “_layouts/folder/customeditapplicationpage.aspx;

    CT1.DisplayFormUrl = “_layouts/folder/customdisplayapplicationpage.aspx;

    //Delete the default from to enable custom application pages.

    CT1.XmlDocuments.Delete(“http://schemas.microsoft.com/sharepoint/v3/contenttype/forms/url”);

    web.AllowUnsafeUpdates = true;

    CT1.Update();

    list.Update();

    And same update for the other content type. Everything works well.

    When we click on new, custom new form opens, when we click on edit, custom edit form opens, when we click on view item, custom view form opens for respective content type. The problem is when we click on linktitle field value in allitems.aspx, it opens up allitems.aspx page instead of opening display form as model dialog which is does when we click on view item by either selecting in ECB menu or from ribbon.

    On custom application pages, I am using SharePoint:SaveButton to save values so that I do not need to write down the code.

    I just tested one thing that, if we have our custom button and if we write traditional way to inserting list item into the list with specifying ContentTypeId in list item, then this problem does not appear. This is when SharePoint:SaveButton is clicked and item is saved, at this time problem appears. I prefer going with SharePoint:SaveButton as I have more than 70 fields on the form and do not want to write a code to set values for 70 fields by doing SPList.AddItem()

    According to me, SharePoint:SaveButton is saving list item inside folder because when we update the view and set to show items without folder, I cannot see list items saved using SharePoint:SaveButton click. But when i switch back to show items with folder, all items appear. This may be the reason when I click on LinkTitle directly on that hyperlink, it opens up AllItems.aspx with FolderCTId={} as one of the query string.

    Any pointers would be highly appreciated.

  2. Hi! Thank you for pointing out this. I have a custom form where I use Indexed = True and AllowDuplicateValues = False on some of the fields. Before I applied the override the form got a notice from the server as postback when there was a duplicate field. Now I only get an ASPX error page with the error message. Do I have to hook back into some validation loop by using your suggestion? I also would like to redirect to another page, is this possible if I use the custom save handler?

Leave a Reply to Malay Vasavada Cancel reply

Your email address will not be published.


*