Skip to content

Instantly share code, notes, and snippets.

@premsh
Created May 16, 2013 22:40
Show Gist options
  • Save premsh/5595654 to your computer and use it in GitHub Desktop.
Save premsh/5595654 to your computer and use it in GitHub Desktop.
create sharepoint group upon feature activation
const string groupName = "CandidateSigned";
// Creates group
public static void CreateCandidateSignedPermissionGroup(SPWeb web, string groupName, string groupDescription)
{
SPUserCollection users = web.AllUsers;
SPUser owner = web.SiteAdministrators[0];
SPMember member = web.SiteAdministrators[0];
SPGroupCollection groups = web.Groups;
groups.Add(groupName, member, owner, groupDescription);
SPGroup newSPGroup = groups[groupName];
SPRoleDefinition role = web.RoleDefinitions.GetByType(SPRoleType.Administrator);
SPRoleAssignment roleAssignment = new SPRoleAssignment(newSPGroup);
roleAssignment.RoleDefinitionBindings.Add(role);
web.RoleAssignments.Add(roleAssignment);
web.Update();
}
// Deletes group
public static void DeleteCandidateSignedPermissionGroup(SPWeb web, string groupName)
{
SPGroupCollection groups = web.SiteGroups;
groups.Remove(groupName);
web.Update();
}
// Uncomment the method below to handle the event raised after a feature has been activated.
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
string groupDescription = "Games Lab verified users";
try
{
using (SPWeb web = properties.Feature.Parent as SPWeb)
{
SPRoleDefinition permissionLevel = web.RoleDefinitions.GetByType(SPRoleType.Contributor);
CreateCandidateSignedPermissionGroup(web, groupName, groupDescription);
}
}
catch (Exception ex)
{
//ex.Message;
}
}
// Uncomment the method below to handle the event raised before a feature is deactivated.
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
try
{
using (SPWeb web = properties.Feature.Parent as SPWeb)
{
DeleteCandidateSignedPermissionGroup(web, groupName);
}
}
catch (Exception ex)
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment