A Practical Guide to SharePoint 2013

A Practical Guide to SharePoint 2013
A Practical Guide to SharePoint 2013 - Book by Saifullah Shafiq

Friday, November 3, 2006

Creating a survey in MOSS 2007 programmatically

Creating a survey in MOSS 2007 programmatically
I wrote about surveys in one of my previous posts. You can read it here:
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 filescommon filesmicrosoft sharedweb server extensions12isapi
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:
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:
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

No comments:

Post a Comment