If you're building custom solutions for SharePoint you probably have various types of users that are accessing the portal with all sorts of permissions on different containers (such as document libraries, lists, etc.). Quite often this means you need to create a custom level of access to the group and define what functions users are allowed to access (such as item creation, editing, etc.).
Here is how to assign all of those settings programatically, so you can call them from the feature receiver when creating a site from your site template:
First we create a custom permission level with the name and description and set of permissions you can find here.
SPRoleDefinition role = new SPRoleDefinition(); role.BasePermissions = SPBasePermissions.OpenItems | SPBasePermissions.EditListItems | SPBasePermissions.ViewListItems | SPBasePermissions.ViewPages | SPBasePermissions.Open | SPBasePermissions.ViewFormPages; role.Name = "My Role Name"; role.Description = "My Role Description"; rootWeb.RoleDefinitions.Add(role);
Then we assign a set of permissions to an existing group in this case called MyGroup
SPRoleAssignment roleAssignment = new SPRoleAssignment(rootWeb.SiteGroups["MyGroup"]); roleAssignment.RoleDefinitionBindings.Add(role); rootWeb.RoleAssignments.Add(roleAssignment);
If you're reading this you might have spent 10 minutes trying to find this particular piece of code. Here is something I found very useful if you're developing for SharePoint 2007 or SharePoint 2010: a great set of SharePoint snippets from CodePlex.
This isn't just one-line-of-code-type snippets (well some are), but they are mostly really handy pieces of functionality, and it's really good to know it's out there when you need it.