How to create a custom thank you dialog in a SharePoint 2010 survey
This blog post describes how to create a custom thank you dialog for all your SharePoint 2010 Surveys. The solution is implemented by the use of Visual Studio and SharePoint Designer. I have found many blog posts and questions in forums on this matter, but none of them seems to have a solution that works on a multiple paged survey with validation.
This solution works on a multiple paged survey with OOTB validation!
1) Create custom survey list definition
This is described in step 1 of this blog post: Lillebuen IT blog post: Hide Save and Close button in Survey
2) Create an item event receiver for the list
- Right click on the list definition folder in Visual Studio
- Choose Add > New Item...
- Choose Even Receiver and give the event receiver a name, e.g. MyEventReceiver
- In the customization wizard:
- Choose List Item Event
- Choose your survey list definition (MySurvey)
- Check "An item is being added" and "An item is being updated"
- Add code to display a dialog after the invoking the base methods
namespace Namespace.ListDefinitions.MySurvey.MyEventReceiver
{
public class MyEventReceiver: SPItemEventReceiver
{
private readonly HttpContext _currentContext;
public SurveyListReceiver()
{
if (HttpContext.Current != null)
{
_currentContext = HttpContext.Current;
}
}
private void DisplayConfirmationMessage()
{
if (_currentContext != null)
{
_currentContext.Response.Write(
"<script type='text/javascript'>alert('We have received your answers. Thank you for your participation.');</script>");
}
}
public override void ItemAdding(SPItemEventProperties properties)
{
base.ItemAdding(properties);
try
{
DisplayConfirmationMessage();
}
catch (Exception ex)
{
// error handling code
}
}
public override void ItemUpdating(SPItemEventProperties properties)
{
base.ItemUpdating(properties);
try
{
DisplayConfirmationMessage();
}
catch (Exception ex)
{
// error handling code
}
}
}
}
I have tried displaying the Thank you text in a SharePoint:UI:Dialog, but I haven't figured out how. It seems like I cannot add script references when doing it like this. Please tell me if anyone have a solution to this.
The reasons for implementing it like this are many:
- There are many blog posts suggesting that the PreSaveAction javascript method can be invoked, but this method is invoked before the OOTB survey validation. A dialog saying thank for your response, with a survey containing errors is quite stupid.
- Others suggest setting the Source in the URL. This works perfectly on a survey with one page. But when a survey contains more than one page, the survey generates more than on response per actual End User response and the thank you page never shows...
- I have also tried doing a redirect in the methods above, but this will cancel the event.
- I have also tried doing a redirect in the ItemAdded and ItemUpdated methods instead, but this is not possible since these methods are asynchronously, itemAdding and itemUpdating however are synchronously.