SharePoint Blogs / SharePoint University
SharePoint Blogs and SharePoint University - all in one place!
Need SharePoint Training? Attend a SharePoint Bootcamp!

Please delete cookies related to sharepointblogs.com and sharepointu.com to resolve login issues!

Creating a survey in MOSS 2007 programmatically
All About SharePoint - S.S. Ahmed - MVP Microsoft SharePoint

Creating a survey in MOSS 2007 programmatically

I wrote about surveys in one of my previous posts. You can read it here:

http://www.sharepointblogs.com/ssa/archive/2006/11/01/creating-a-survey-in-moss-2007.aspx

I showed you how you can create a survey in MOSS 2007. I demonstrated how you can use branching to skip specific questions and make your surveys meaningful. Today we will see how one can add a survey programmatically. It is very easy. Just a few lines in fact! Here is the code:

using Microsoft.SharePoint;
.
.
.

SPSite site = new SPSite("http://sp:81/SiteDirectory/site1");

SPWeb web = site.OpenWeb();

string surveyname = "Survey2";

SPListTemplate listTemplate = web.ListTemplates["Survey"];

System.Guid guid = web.Lists.Add(surveyname,"survey",listTemplate);

Note to Beginners: Don't forget to add reference to Microsoft.SharePoint.DLL. You will find this DLL in the following folder:

c:\program files\common files\microsoft shared\web server extensions\12\isapi\

The following line will add the new survey:

System.Guid guid = web.Lists.Add(surveyname,"survey",listTemplate);

First parameter is the survey's title, second parameter is description and the third is the template. This line will return a Guid. You can access a list by using either it's title or it's guid. Now that you have added a new survey in your site, you may also want to add users programmatically. There are two ways to add a new user. You can use obsolete methods. For example, have a look at the following lines:

SPList survey = web.Lists["survey2"]; //survey2 is survey's title

SPMember member = web.Users["sp\\user1"]; //This user must exist in the parent site.

survey.Permissions.Add(member, SPRights.ManageLists);

This will add the user to the survey but here is what you will see on the screen:

The user has been added but when you view the user's permissions, you see a strange permission in the list. Permission is:

Auto-generated Permission Level 6asdf-4343-4235-f334-af342352355 - This permission level is automatically generated when the obsolete SPPermissionCollection class is used.

This happened because we used obsolete methods. Permissions.Add() is not supported any more. Similarly, SPRights.ManageLists is not supported. The new method of adding a user is to use SPRoleAssignment class. Here is how you do it:

SPRoleAssignment roleAssignment = new SPRoleAssignment("SP\\user1", "user1@localhost", "User1", "Notes");

SPRoleDefinition RoleDefinition = web.RoleDefinitions.GetByType(SPRoleType.Contributor);

roleAssignment.RoleDefinitionBindings.Add(RoleDefinition);

//Check inheritance

if (!survey.HasUniqueRoleAssignments)

{

survey.BreakRoleInheritance(true);

}

survey.RoleAssignments.Add(roleAssignment);

survey.Update();

MessageBox.Show("User successfully added!");

You can read more about SPRoleAssignment class on the following page:

http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.sproleassignment.aspx

Let's analyze the code:

SPRoleAssignment roleAssignment = new SPRoleAssignment("SP\\user1", "user1@localhost", "User1", "Notes");

First parameter is user login, second is user email, third is user's name and last one is comments or notes.

SPRoleDefinition RoleDefinition = web.RoleDefinitions.GetByType(SPRoleType.Contributor);

We have selected "Contributor" role for the user. You can select any role from the following list:

1. Administrator
2. Contributor
3. Guest
4. None
5. Reader
6. WebDesigner

Add role definition binding:

roleAssignment.RoleDefinitionBindings.Add(RoleDefinition);

You must use following lines to check inheritance from the parent site:

if (!survey.HasUniqueRoleAssignments)
{

    survey.BreakRoleInheritance(
true);

}

Check if user is inheriting permissions from the parent site. You can check this with "HasUniqueRoleAssignments" property. [Reference: http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.splist.hasuniqueroleassignments.aspx]

Use "BreakRoleInheritance()" method to assign permissions to the user. This method accepts a boolean value. If you pass "True", role assignments will be copied from the parent site and if you pass "False", then you can assign new permissions. [Reference: http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.splist.breakroleinheritance.aspx]

If you don't use this check, you won't get an error when you run the code for the first time but second time, you will get a run time error. So, it is better to use the check to avoid any runtime errors.

Here are the final lines:

survey.RoleAssignments.Add(roleAssignment); //Add roleAssignment

survey.Update(); //Apply update

MessageBox.Show("User successfully added!"); //Display a success message

You can download application (source code) in RAR format from the following location:

http://www.walisystems.com/articles/sps/moss-surveys/create-a-survey/Survey.rar [Size: 279 Kb]

Open your site and view newly added survey and user.

1. Open site and click on the newly added survey (under Site Hierarchy).

2. Click "Settings > Survey Settings" to open settings page.

3. Click "Permissions for this survey" link to open the permissions page.

4. You will find your newly added user along with his permissions on this page.

Please feel free to send your comments and suggestions at share.point@yahoo.com

Thank you.

-SSA


Posted 11-03-2006 10:12 PM by ssa

Comments

All About SharePoint - S.S. Ahmed - MVP Microsoft SharePoint wrote Enabling Anonymous access in surveys
on 12-25-2007 2:56 AM

I am asked quite often about the anonymous access in surveys. This may be because I wrote a couple articles

maideen wrote re: Creating a survey in MOSS 2007 programmatically
on 12-25-2007 11:16 PM

I am asked to develop a web part to list down all the questions in a survey.Is that possible.

Rania Badawi wrote re: Creating a survey in MOSS 2007 programmatically
on 05-26-2008 11:35 AM

Any idea how to customize or get ride of the runtime error.

"You are not allowed to respond again to this survey"?

rania_badawi@hotmail.com

Guapo Yerby wrote re: Creating a survey in MOSS 2007 programmatically
on 07-08-2008 7:57 PM

I didn't see anything on how to create the questions in the survey. I need to create branching questions programmatically. Anyone have experience doing this?

Create user programmatically | keyongtech wrote Create user programmatically | keyongtech
on 01-18-2009 10:36 AM

Pingback from  Create user programmatically | keyongtech

Maddy wrote re: Creating a survey in MOSS 2007 programmatically
on 01-30-2009 4:32 AM

Can you please explain creating a survey in publishing site template site collection programmatically?

We can create survey in team site template but throughs an error when you try to create in publishing feature template?

Kota wrote re: Creating a survey in MOSS 2007 programmatically
on 02-16-2009 11:41 PM

very informative for d beginners.

i had a question.

I created a survey.

to my site i wanna to display the user information when v click on "Show all responses" how i can do this?

when ever a user login to my site am going to use windows authentication.

Add a Comment

(required)  
(optional)
(required)  
Remember Me?
Need SharePoint Training? Attend a SharePoint Bootcamp!
Posts (c) their respective authors. Everything else (c) 2009 SharePoint Experts, Inc.