At work they wanted me to create a 2 question survey that allowed the users to post as many times as they wanted anonymously on multiple sites. I had created a document library on multiple sites using a console application, but I had never created a survey. They also wanted there domain users to have access to the survey, so that they could post and not delete or edit the surveys plus a couple of alerts setup. The sites already existed and were being created from a template, so I just updated the template initially. The second part dealt with some custom code that I wrote against this custom survey template. Here is an example of how you would create off of a custom survey template, add the permissions, then add the alerts using the SharePoint Object Model:
string listName = "Rate This Site";
string listDescription = "Site Rating.";
try
{
SPListTemplateCollection SurveyTemplateCollection =
site.GetCustomListTemplates(WebName);
SPListTemplate SurveyTemplate = SurveyTemplateCollection["Survey Template Name"];
//creates survey using custom template, must do an update on the web before making changes
//i.e. adding alerts and permissions
WebName.Lists.Add(listName, listDescription, SurveyTemplate);
WebName.Update();
SPList ListName= WebName.Lists[List Name];
//add a SharePoint User Group to the list and does not inherit permissions from the site
ListName.BreakRoleInheritance(false); //set to true if you are only adding a group
SPGroup RegularUser = WebName.SiteGroups["Group Name"];
SPRoleDefinition RoleDefinition;
SPRoleAssignment RoleAssignment;
RoleDefinition = WebName.RoleDefinitions["Permission Name"];
RoleAssignment = new SPRoleAssignment(RegularUser);
RoleAssignment.RoleDefinitionBindings.Add(RoleDefinition);
ListName.RoleAssignments.Add(RoleAssignment);
SPGroup GroupName = ChangeNAPWeb.SiteGroups[Group Name];
foreach (SPUser UserName in GroupName.Users)
{
//setup alerts for a specific user
if (UserName.LoginName == "username with domain in caps")
//domain name is always in caps
{
SPAlert alert = WebName.Alerts.Add();
alert.AlertType = SPAlertType.List;
alert.Title = "Title of Survey";
alert.EventType = SPEventType.All;
alert.AlertFrequency = SPAlertFrequency.Immediate; //this is for immediate alerts
alert.User = UserName
alert.List = List;
alert.Status = SPAlertStatus.On;
alert.Update(true);
}
}
localList.Update(); //if you don't include this statement then the survey will not be updated
}
catch(Exception ex)
{
}
If you would like any additional information or you have a comment or question let me know. I believe on my old blog there is an entry on how you would set this up in a Console Application and how you could add information to the quick launch bar programmatically. If you don't like how it adds the survey to the top of the quick launch bar you can add the following code: ListName.OnQuickLaunch = false;
Then you can go back to my old post and look at the code on how to add the link to the point in the quicklaunch where you would like. I hope that you enjoy the code.