Skip to content
Amal Hashim
All posts
Article

SharePoint Survey List - EventReceiver

Run code when a survey response is finished by handling ItemAdded and ItemUpdated and checking the Completed field.

2 min read#C##SharePoint

A survey list behaves differently from other lists. A survey can have many questions spread over several pages, so one response is saved and then updated as the user moves along.

If you need to do calculations once a response is complete, an event receiver is the best option. You must handle both ItemAdded and ItemUpdated.

How to detect a finished response

The Completed field holds 255 while the user is still answering. When the user clicks Finish, it changes to 1. So the receiver should act only when the value equals 1.

Template

csharp
using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;

namespace Survey
{
    /// <summary>
    /// List Item Events
    /// </summary>
    public class SurveyListReceiver : SPItemEventReceiver
    {
        /// <summary>
        /// An item was added.
        /// </summary>
        public override void ItemAdded(SPItemEventProperties properties)
        {
            try
            {
                if (properties.ListItem["Completed"] != null && properties.ListItem["Completed"].ToString() == "1")
                {
                    // Survey is complete. Do the action
                }
            }
            catch
            {
            }
            base.ItemAdded(properties);
        }

        /// <summary>
        /// An item was updated.
        /// </summary>
        public override void ItemUpdated(SPItemEventProperties properties)
        {
            try
            {
                if (properties.ListItem["Completed"] != null && properties.ListItem["Completed"].ToString() == "1")
                {
                    // Survey is complete. Do the action
                }
            }
            catch
            {
            }
            base.ItemUpdated(properties);
        }
    }
}