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:



//ASPX PAGE

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

<sharepoint:savebutton   runat="server" ControlMode="New"  ID="savebutton" ></sharepoint:savebutton>

//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;

...
}
}


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.

Karsten Pohnke About Karsten Pohnke
He is Consultant for SharePoint Solutions for collaboration, communication and business processes. He provides his customers applications based on standard features as well as development or combining the power of several microsoft tools like Dynamics CRM. In his free time he tries to collect tipps and worthy experience in this blog.

3 comments on “SharePoint Override save button in list form by using csharp

  1. Malay Vasavada

    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. Eirik Johannessen

    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?

Submit comment

Allowed HTML tags: <a href="http://google.com">google</a> <strong>bold</strong> <em>emphasized</em> <code>code</code> <blockquote>
quote
</blockquote>

Please fill in the captcha: * Time limit is exhausted. Please reload CAPTCHA.