Set of code to do some cool things programatically…
SPSite site = new SPSite("http://sharepoint2007:100");
site.AllowUnsafeUpdates = true;
SPWeb web = site.RootWeb;
web.AllowUnsafeUpdates = true;
SPListTemplateCollection coll = web.ListTemplates;
SPList newList = null;
//create a list
try
{
newList = web.Lists[web.Lists.Add("Assignments", "Student Assignments", coll[1])];
}
catch (Exception ex)
{
newList = web.Lists["Assignments"];
}
string[] users = {"training\administrator","training\student"};
//iterate all the users (from database?)
foreach (string s_user in users)
{
//add to site
SPUser user = web.EnsureUser(s_user);
string name = s_user;
if ( user.LoginName.Contains("\"))
name = user.LoginName.Substring(user.LoginName.IndexOf("\") + 1);
//create a folder for each user
SPListItem folder = newList.Items.Add("", SPFileSystemObjectType.Folder,name);
try
{
folder.Update();
newList.Update();
}
catch (Exception ex)
{
}
//set permissions
SPRoleDefinition RoleDefinitionRdr = web.RoleDefinitions.GetByType(SPRoleType.Administrator);
SPRoleAssignment roleAssignment = new SPRoleAssignment((SPPrincipal)user);
roleAssignment.RoleDefinitionBindings.Add(RoleDefinitionRdr);
//adds permissions to site
web.RoleAssignments.Add(roleAssignment);
if (!folder.HasUniqueRoleAssignments)
{
folder.BreakRoleInheritance(false);
}
while (folder.RoleAssignments.Count > 0)
folder.RoleAssignments.Remove(0);
folder.RoleAssignments.Add(roleAssignment);
folder.Update();
}