Tuesday, August 19, 2008

Adding survey list programmatically

Hi All,

We all know the surveys are really very important part of many companies. They take the decision based on the survey and only because of this reason MOSS has come up with this facility. We can create a survey from MOSS GUI.

There is also a way we can create a survey programmatically. I would like to demonstrate this.

Let’s develop a code which creates a survey list and also adds the question to it. I have added only one question. You can add any number of questions you want.

SPListTemplate objTemplate = currentWeb.ListTemplates["Survey"];

Guid objGuid = currentWeb.Lists.Add("ServeyFromCode", "survey", objTemplate);

SPList objSurvey = currentWeb.Lists["ServeyFromCode"];

//Let’s add user for the permission

SPMember objMember = currentWeb.Users[“Domain/UserName”];

objSurvey.Permissions.Add(objMember, SPRights.ManageLists);

StringCollection strQuestions = new StringCollection();

string strQuestion = "What should be the timing for the training?";

strQuestions.Add("10 AM - 12 AM");
strQuestions.Add("3 PM - 5 PM");
strQuestions.Add("7 PM - 9 PM");
strQuestions.Add(" 9PM - 11 PM");

objSurvey.Fields.Add(strQuestion, SPFieldType.Choice, true, false, strQuestions);

objSurvey.AllowMultiResponses = false;


Before you write this code please set AllowunsafeUpdates = true for list as well as for web and then after updating list please set it to false again.

When you add this, you will see one more list being added in your site and you can now use that survey


heena said...

its nice to have atleast one coding about survey. i been trying to add custom dropdown box in one of the answere in the survey. i am having difficulty doing that, not only that i couldnt find the code to change the survey. can you plese help me on that. i having big time for this problem.
Thank you

Malay Vasavada said...

Hi Heena,

I am actually not able to figure out what you are trying to say. but if i understood it correct, then this code actually adds the drop down for the question.

What do you really mean by changing the survey, Please explain me in detail.

Please let me know if you meant to ask anything else, i will come back to you as soon as possible.

K.D.Kadyan said...

Hey Nice blog,

I want that user should be able to respond by my webpart is this possible??

Like in my webpart all question will be same like existing survey but on submit click i want some extra facility for client. Is this possible???

Kuldeep Kadyan

JC said...

What properties do I use to set the default choice answer and Set the question as required?
jc (at) cyberpine (dot) com

SharePoint Kings said...

@JC, not sure about it how to do it as we had not done it yet.

we will try to do it if got time, sorry about that.

if you got it please let us and other user know as well.

Renato said...

Great post! But can we make it radio buttons not allowing multiple choices?


Share your SharePoint Experiences with us...
As good as the SharePointKings is, we want to make it even better. One of our most valuable sources of input for our Blog Posts comes from ever enthusiastic Visitors/Readers. We welcome every Visitor/Reader to contribute their experiences with SharePoint. It may be in the form of a code stub, snippet, any tips and trick or any crazy thing you have tried with SharePoint.
Send your Articles to sharepointkings@gmail.com with your Profile Summary. We will Post them. The idea is to act as a bridge between you Readers!!!

If anyone would like to have their advertisement posted on this blog, please send us the requirement details to sharepointkings@gmail.com