Create SharePoint Survey list and set properties using C#.Net Code

We know, Surveys are very important for many organizations, by conducting survey they take the decisions based on the responses. SharePoint is providing a good flexibility for Surveys.
I have a user requirement, user will deploy a single WSP file in different Servers and that WSP file should deploy all the SharePoint Lists in that environment. Creating SharePoint survey list programmatically is very easy. Lets see how to create a SharePoint survey list using programming and set survey list properties in C#.Net.

C#.NET Code Behind:

  //Adding Survey List to SharePoint site
  SPListTemplate objTemplate = web.ListTemplates["Survey"];
  Guid objGuid = web.Lists.Add(strListName, string.Empty, objTemplate);
  SPList objSurvey = web.Lists[strListName];

  //Changing the title of the SharePoint List
  objSurvey.Title = Constants.ListTitles.VotesForLecturers;

  //Changing the SharePoint Survey list property to allow multiple responses
  objSurvey.AllowMultiResponses = true;

  //Adding lookup column to survey List Programmatically
  SPList lstEmployees = web.Lists[Constants.ListNames.Employees];
  var Employees = objSurvey.Fields.AddLookup("Employee", lstEmployee.ID, true);
  SPFieldLookup EmployeesField = (SPFieldLookup)objSurvey.Fields[Employees];
  EmployeesField.LookupField = lstEmployees.Fields["Employee Name"].InternalName;
  EmployeesField.Update();

  //Creating and Adding Question to Survey List Programmatically with rating scale
  strQuestionTitle = "Lecturer Skills";

  //Adding Question Options
  StringCollection strQuestions = new StringCollection();
  strQuestions.Add("Approachability");
  strQuestions.Add("Availability");
  strQuestions.Add("Share Knowledge");
  strQuestions.Add("Motivate");
  strQuestions.Add("Provide guidance");

  //Adding Question field to survey list
  objSurvey.Fields.Add(strQuestionTitle, SPFieldType.Choice, true, false, strQuestions);

  //Adding text field to Survey list
  objSurvey.Fields.Add("Comments", SPFieldType.Note, false);
  objSurvey.Update();

You can also Add Rating scale to the survey List.

Before your code use "AllowUnsafeUpdates = true" for updating the web and then after adding the Survey list to web, reset this property back to false.

Note:
Before running this code, you have to ensure the "Team Collaboration Lists" feature is activated.

Activating Team Collaboration Lists Feature: to activate this feature, navigate to 'Site settings' > 'Site Features' > scroll down the list and ensure this feature is active.

You have successfully added a survey list to SharePoint site after successfully deploying the code. you can navigate to site content to see this list.

Hope this article is helpful for you.
Create SharePoint Survey list and set properties using C#.Net Code Create SharePoint Survey list and set properties using C#.Net Code Reviewed by Sudheer Gangumolu on November 25, 2014 Rating: 5

3 comments:

  1. Most property management companies do not understand just how important the home page is in advertising rental properties. Understanding what is important and how it should be organized is critical to the leasing success management companies and portfolio owners operating the website. macpherson mall

    ReplyDelete
  2. I was surfing net and fortunately came across this site and found very interesting stuff here. Its really fun to read. I enjoyed a lot. Thanks for sharing this wonderful information. el gouna white villas for sale

    ReplyDelete
  3. Want to find legitimate paid survey sites you can join to make money? Learn how to identify and find reputable survey companies and avoid the scams. kroger feedback

    ReplyDelete