internal static SPGroup AddSecurityGroup(SPGroupCollection groups, string name, string description, SPUser owner, bool addOwnerToGroup) { SPGroup item = null; try { item = groups[name]; if (addOwnerToGroup) { item.AddUser(owner); } } catch (SPException ex) { if (-2146232832 == ex.ErrorCode) { SPMember associatedOwnerGroup = owner; if (groups.Web.AssociatedOwnerGroup != null) { associatedOwnerGroup = groups.Web.AssociatedOwnerGroup; } if (addOwnerToGroup) { groups.Add(name, associatedOwnerGroup, owner, description); } else { groups.Add(name, associatedOwnerGroup, null, description); } item = groups[name]; } else { SPDiagnosticsService.Local.WriteTrace(0, new SPDiagnosticsCategory(ex.Source, TraceSeverity.High, EventSeverity.Error), TraceSeverity.High, ex.Message, ex.Data); } } IList <SPGroup> associatedGroups = item.ParentWeb.AssociatedGroups; if ((item != null) && (associatedGroups != null)) { bool flag = true; foreach (SPGroup group2 in associatedGroups) { if (group2.ID == item.ID) { flag = false; break; } } if (flag) { associatedGroups.Add(item); } } return(item); }
private void CreateSubSiteGroup(SPWeb web, string groupName, SPRoleType roleType, string groupDescription) { var groupInSite = GroupExistsInSiteCollection(web, groupName); if (!groupInSite) { web.AllowUnsafeUpdates = true; web.Update(); web.BreakRoleInheritance(false); SPGroupCollection groups = web.SiteGroups; //for (int i = 0; i < web.SiteAdministrators.Count; i++) //{ // SPUser owner = web.SiteAdministrators[i]; // SPMember member = web.SiteAdministrators[i]; //} SPUser owner = web.SiteAdministrators[0]; SPMember member = web.SiteAdministrators[0]; groups.Add(groupName, member, owner, groupDescription); SPGroup newSPGroup = groups[groupName]; SPRoleDefinition role = web.RoleDefinitions.GetByType(roleType); SPRoleAssignment roleAssignment = new SPRoleAssignment(newSPGroup); roleAssignment.RoleDefinitionBindings.Add(role); web.RoleAssignments.Add(roleAssignment); web.Update(); } }
public static void CreateSubSiteGroup(SPWeb web, string groupName, string PermissionLevel, string groupDescription) { try { web.AllowUnsafeUpdates = true; SPUserCollection users = web.AllUsers; SPUser owner = web.SiteAdministrators[0]; SPMember member = web.SiteAdministrators[0]; SPGroupCollection groups = web.SiteGroups; groups.Add(groupName, member, owner, groupDescription); SPGroup newSPGroup = groups[groupName]; newSPGroup.OnlyAllowMembersViewMembership = false; newSPGroup.Update(); SPRoleDefinition role = web.RoleDefinitions[PermissionLevel]; SPRoleAssignment roleAssignment = new SPRoleAssignment(newSPGroup); roleAssignment.RoleDefinitionBindings.Add(role); web.RoleAssignments.Add(roleAssignment); web.Update(); web.AllowUnsafeUpdates = false; } catch (Exception ex) { ULSLogger.LogErrorInULS(ex.Message, TraceSeverity.Unexpected); } }
// Uncomment the method below to handle the event raised after a feature has been activated. public override void FeatureActivated(SPFeatureReceiverProperties properties) { SPSite currentSite = properties.Feature.Parent as SPSite; SPWeb rootWeb = currentSite.RootWeb; SPUser currentUser = rootWeb.CurrentUser; SPGroupCollection groups = rootWeb.SiteGroups; groups.Add("Training Administrators", currentUser, currentUser, "All Globomantics Training Administrators are in this group"); SPGroup trainingAdmins = groups["Training Administrators"]; groups.Add("Trainers", trainingAdmins, currentUser, "All Globomantics Trainers are in this group"); groups.Add("Students", trainingAdmins, currentUser, "All Globomantics students are in this group"); SPRoleDefinition fullControl = rootWeb.RoleDefinitions["Full Control"]; SPRoleDefinition contribute = rootWeb.RoleDefinitions["Contribute"]; SPRoleDefinition read = rootWeb.RoleDefinitions["Read"]; SPRoleAssignment trainersRoleAssignment = new SPRoleAssignment(groups["Trainers"]); trainersRoleAssignment.RoleDefinitionBindings.Add(contribute); rootWeb.RoleAssignments.Add(trainersRoleAssignment); SPRoleAssignment studentsRoleAssignment = new SPRoleAssignment(groups["Students"]); studentsRoleAssignment.RoleDefinitionBindings.Add(read); rootWeb.RoleAssignments.Add(studentsRoleAssignment); SPList registrationsList = rootWeb.Lists["Registrations"]; registrationsList.BreakRoleInheritance(true); SPRoleAssignment registrationsRoleAssignment = new SPRoleAssignment(groups["Students"]); registrationsRoleAssignment.RoleDefinitionBindings.Add(contribute); registrationsList.RoleAssignments.Add(registrationsRoleAssignment); SPRoleAssignment trainingAdminsRoleAssignment = new SPRoleAssignment(trainingAdmins); trainingAdminsRoleAssignment.RoleDefinitionBindings.Add(fullControl); rootWeb.RoleAssignments.Add(trainingAdminsRoleAssignment); }
/// <summary> /// Creates a group, if the group does not already exist. Sets the following properties: /// AllowRequestToJoinLeave = true /// OnlyAllowMembersViewMembership = true /// AllowMembersEditMembership = false /// </summary> /// <param name="web">The web where the group will be created</param> /// <param name="groupName">The name of the group</param> /// <param name="groupDescription">The group description</param> /// <param name="owner">The group's owner</param> public static SPGroup CreateGroup(this SPWeb web, string groupName, string groupDescription, SPMember owner) { SPGroup group; if (web.TryGetGroup(groupName, out group) == false) { SPGroupCollection gc = web.SiteGroups; gc.Add(groupName, owner, web.Site.Owner, groupDescription); group = gc[groupName]; group.AllowRequestToJoinLeave = false; group.OnlyAllowMembersViewMembership = true; group.AllowMembersEditMembership = false; group.Update(); } return(group); }
/// <summary> /// Add group to SPWeb and set permission for this group /// </summary> /// <param name="web"></param> /// <param name="groupName"></param> /// <param name="groupDescription"></param> /// <param name="role"></param> /// <param name="member"></param> public static void CreateNewGroup(this SPWeb web, string groupName, string groupDescription, SPRoleType role) { if (string.IsNullOrEmpty(groupName)) { return; } try { SPSecurity.RunWithElevatedPrivileges(delegate() { using (SPSite spSite = new SPSite(web.Site.ID)) { using (SPWeb spWeb = spSite.OpenWeb(web.ID)) { SPUserCollection users = spWeb.AllUsers; SPUser owner = users[spWeb.Author.LoginName]; SPMember member = users[spWeb.Author.LoginName]; //Add the group to the SPWeb web SPGroupCollection groups = spWeb.SiteGroups; var isExits = groups.Cast <SPGroup>().FirstOrDefault(p => p.Name == groupName); if (isExits == null) { groups.Add(groupName, member, owner, groupDescription); } //Associate the group with SPWeb //spWeb.AssociatedGroups.Add(spWeb.SiteGroups[groupName]); spWeb.Update(); //Assignment of the roles to the group. SPRoleAssignment assignment = new SPRoleAssignment(spWeb.SiteGroups[groupName]); SPRoleDefinition _role = spWeb.RoleDefinitions.GetByType(role); assignment.RoleDefinitionBindings.Add(_role); spWeb.RoleAssignments.Add(assignment); } } }); } catch { // Not catch exception because check group exists } }
public void AddGroup(string groupName, string owner, string defaultUser, string description, UserObject.RoleDefinition role) { if (string.IsNullOrEmpty(groupName)) { return; } if (string.IsNullOrEmpty(owner)) { return; } if (string.IsNullOrEmpty(defaultUser)) { return; } try { using (SPWeb website = GetWebSite()) { SPUser sharepointOwner = GetUserObject(owner); if (sharepointOwner == null) { return; } SPUser sharepointDefaultUser = GetUserObject(defaultUser); if (sharepointDefaultUser == null) { return; } SPGroupCollection sharepointGroups = website.SiteGroups; sharepointGroups.Add(groupName, sharepointOwner, sharepointDefaultUser, description); AssignPermission(role, groupName, string.Empty); website.Update(); } } catch (Exception ex) { var log = new AppEventLog(AppException.ExceptionMessage(ex, "AddGroup", "ClsHelper")); log.WriteToLog(); } }
public static void CreateNewGroup(this SPWeb web, string groupName, string groupDescription) { if (string.IsNullOrEmpty(groupName)) { return; } try { SPSecurity.RunWithElevatedPrivileges(delegate() { SPUserCollection users = web.AllUsers; SPUser owner = users[web.Author.LoginName]; SPMember member = users[web.Author.LoginName]; try { //Add the group to the SPWeb web SPGroupCollection groups = web.SiteGroups; groups.Add(groupName, member, owner, groupDescription); //Associate the group with SPWeb web.AssociatedGroups.Add(web.SiteGroups[groupName]); web.Update(); } catch { } //Assignment of the roles to the group. SPRoleAssignment assignment = new SPRoleAssignment(web.SiteGroups[groupName]); SPRoleDefinition _role = web.RoleDefinitions.GetByType(SPRoleType.Contributor); assignment.RoleDefinitionBindings.Add(_role); web.RoleAssignments.Add(assignment); }); } catch { // Not catch exception because check group exists } }
/// <summary> /// Adds SharePoint group to SharePoint v3 site /// </summary> /// <param name="SiteURL">SharePoint v3 site URL</param> /// <param name="GroupName">Group to be added</param> /// <param name="GroupOwner">Group owner. If the user does not belong to site it will be added.</param> /// <param name="DefaultUser">Default group user. If the user does not belong to site it will be added.</param> /// <param name="OverwriteIfExists">If set, the group will be removed and created again, if it exists.</param> /// <param name="ResultText">The method returns log of operations performed.</param> /// <returns>true if the group is successfully added.</returns> public bool CreateUsersGroup(string siteURL, string groupName, CS4User groupOwner, CS4User defaultUser, bool overwriteIfExists, ref string resultText) { bool result = false; resultText = String.Format(TextResources.AttemptingToCreateGroup, groupName) + Environment.NewLine; SPSite site = null; SPWeb siteWeb = null; SPGroupCollection groups = null; try { site = new SPSite(siteURL); siteWeb = site.OpenWeb(); groups = siteWeb.SiteGroups; } catch (System.Exception ex) { resultText += TextResources.CantCreateGroup + ex.Message + System.Environment.NewLine; return(result); } SPUser user = null; bool createDefaultUser = false; try { user = siteWeb.SiteUsers[defaultUser.UserLoginWithDomain]; } catch { //user does not exist createDefaultUser = true; } SPMember owner = null; bool createGroupOwner = false; try { owner = siteWeb.SiteUsers[groupOwner.UserLoginWithDomain]; } catch { createGroupOwner = true; } try { if (createDefaultUser) { AddSiteUser(defaultUser); user = siteWeb.SiteUsers[defaultUser.UserLoginWithDomain]; } if (createGroupOwner) { AddSiteUser(groupOwner); owner = siteWeb.SiteUsers[groupOwner.UserLoginWithDomain]; } } catch (System.Exception ex) { resultText += TextResources.NoGroupOwnerOrDefaultUser + ex.Message + Environment.NewLine; return(result); } SPGroup group = null; bool groupExists = false; try { group = groups[groupName]; groupExists = true; resultText += TextResources.GroupExists + Environment.NewLine; } catch { //group does not exist } try { if (groupExists) { if (overwriteIfExists) { SPUserCollection groupUsers = group.Users; //emptying group users collection foreach (SPUser groupUser in groupUsers) { group.RemoveUser(groupUser); } groups.Remove(groupName); } else { result = true; return(result); } } groups.Add(groupName, owner, user, String.Empty); resultText += TextResources.GroupCreated + System.Environment.NewLine; result = true; } catch (System.Exception ex) { resultText += ex.Message; } return(result); }