Read Sharepoint 2007 Scale Survey with some statistics

Just an hour before letting the employees hit the survey link on our Sharepoint server, the department changed their mind and they doesn’t want the Single Choice for each question Survey I did. They wanted to change the type of the survey from Multiple radio buttons choices for each question(Which returns one Choice) to Scale Survey (GridChoice Survey in Sharepoint terms or Rating Survey) where the vote is of 1 to 5 scale for each point in a question.

Sharepoint does have a Scale Survey, but if you glimps at it you can’t tell who got the best score, which is something they wanted for the “Employee of the month”.

So in the same way I did to read the items of the Choice Survey, I did it again with some modifications. So instead of looking for the “Choice” type, I used “GridChoice”. Also, the value of the response is different. In a GridChoice value you woud see something like:

Co-operation#4#Skills#3#Outgoing#4#Ideas#3#Attendance#2#

This means the employee got a vote of 4 out of 5 for Co-operation,3 for Skills,4 for Outgoing,3 for Ideas and 2 for Attendance.

So now all I had to do is find a way to extract the numbers out of the string and sum up the numbers for each employe. To do that I did the following C# function:


  private int ResponceScore(string expr)
  {
  String[] numbers = System.Text.RegularExpressions.Regex.Split(expr, @"[^\d+]");
  int total = 0;
  foreach (String number in numbers)
  {
  if (number != "")
  {
  int num = int.Parse(number);
  total += num;
  }
  }
  return total;
  }

What the funtion does is it recieves the response’s text, use a regular expression to select or extract the numbers in the string, sum up the numbers of the response and send it back to be saved in an array or in a variable (Like adding to a variable for each employee).

So the final code is something like:

 

foreach (SPListItem item in SurveyList.Items)
  {
  foreach (SPField field in item.Fields)
  {
  if (field.TypeAsString == “GridChoice”)
  {
  if (field.Title.ToString() == “Employee Name”)
  {
  empScore+= ResponceScore(item[field.Title].ToString());
  }
}
}

So that is how I managed to do a web part to show the top scores in a Scale Survey in Sharepoint.

Have fun 😉
kick it on DotNetKicks.com

Share