// Private Methods (1) private void AddGroup(SPWeb spWeb, SPGroup owner, SPUser user, string groupName) { LogMessage(groupName, 3); try { SPGroup spGroup = null; try { spGroup = spWeb.SiteGroups.GetByName(groupName); } catch { } if (spGroup == null) { spWeb.SiteGroups.Add(groupName, owner, user, null); SPRole roll = spWeb.Roles["Read"]; roll.AddGroup(spWeb.SiteGroups[groupName]); try { SPList spList = spWeb.Lists["User Information List"]; spList.Items.GetItemById(spWeb.SiteGroups[groupName].ID).SystemUpdate(); } catch { } LogMessage(null, MessageKind.SUCCESS, 4); } else { LogMessage("Group already exists.", MessageKind.SKIPPED, 4); } } catch (Exception exception) { LogMessage(exception.Message, MessageKind.FAILURE, 4); } }
public static Dictionary <string, SPRoleType> AddBasicSecurityToWorkspace(SPWeb eleWeb, string safeTitle, SPUser owner) { var safeGroupTitle = string.Empty; safeGroupTitle = CoreFunctions.GetSafeGroupTitle(safeTitle); eleWeb.AllowUnsafeUpdates = true; Dictionary <string, SPRoleType> pNewGroups = new Dictionary <string, SPRoleType>(); string[] grps = new[] { "Owner", "Member", "Visitor" }; SPGroup ownerGrp = null; // add groups and set group owners foreach (string grp in grps) { string finalName = string.Empty; SPGroup testGrp = null; try { testGrp = eleWeb.SiteGroups[safeGroupTitle + " " + grp]; } catch { } //Commented code is no longer use and commented to resolve Jira Id 2321 //if (testGrp != null) //{ // finalName = testGrp.Name; //} //else //{ try { finalName = CoreFunctions.AddGroup(eleWeb, safeGroupTitle, grp, owner, eleWeb.CurrentUser, string.Empty); eleWeb.Update(); } catch { } //} SPGroup finalGrp = eleWeb.SiteGroups[finalName]; SPRoleType rType; switch (grp) { case "Owner": ownerGrp = finalGrp; rType = SPRoleType.Administrator; finalGrp.Owner = owner; finalGrp.AddUser(owner); finalGrp.Update(); eleWeb.Update(); break; default: rType = SPRoleType.Reader; finalGrp.Owner = ownerGrp; finalGrp.Update(); eleWeb.Update(); break; } pNewGroups.Add(finalGrp.Name, rType); } // now set groups to become web's owner, member, visitor SPGroup group = null; SPRole roll = null; foreach (KeyValuePair <string, SPRoleType> g in pNewGroups) { if (g.Key.Contains("Owner")) { group = eleWeb.SiteGroups[g.Key]; eleWeb.AssociatedOwnerGroup = group; roll = eleWeb.Roles["Full Control"]; roll.AddGroup(group); } else if (g.Key.Contains("Member")) { group = eleWeb.SiteGroups[g.Key]; eleWeb.AssociatedOwnerGroup = group; roll = eleWeb.Roles["Contribute"]; roll.AddGroup(group); } else if (g.Key.Contains("Visitor")) { group = eleWeb.SiteGroups[g.Key]; eleWeb.AssociatedOwnerGroup = group; roll = eleWeb.Roles["Read"]; roll.AddGroup(group); } } eleWeb.Update(); return(pNewGroups); }
public void setUpGroups() { SPSecurity.RunWithElevatedPrivileges(delegate() { using (SPSite site = new SPSite(myWebToPublish.Url)) { using (SPWeb web = site.OpenWeb()) { SPUser owner = null; try { owner = web.SiteUsers[_username]; }catch {} string aowner = ""; string amember = ""; string avisitor = ""; string strEPMLiveGroupsPermAssignments = ""; Guid lockweb = EPMLiveCore.CoreFunctions.getLockedWeb(web); if (lockweb != Guid.Empty) { using (SPWeb lweb = web.Site.OpenWeb(lockweb)) { aowner = EPMLiveCore.CoreFunctions.getConfigSetting(lweb, "EPMLiveNewProjectRoleOwners"); amember = EPMLiveCore.CoreFunctions.getConfigSetting(lweb, "EPMLiveNewProjectRoleMembers"); avisitor = EPMLiveCore.CoreFunctions.getConfigSetting(lweb, "EPMLiveNewProjectRoleVisitors"); strEPMLiveGroupsPermAssignments = EPMLiveCore.CoreFunctions.getConfigSetting(lweb, "EPMLiveGroupsPermAssignments"); } } web.Update(); //=========Owner Group======================== SPRole roll = web.Roles["Full Control"]; try { if (aowner != "") { web.AssociatedOwnerGroup = web.SiteGroups.GetByID(int.Parse(aowner)); roll.AddGroup(web.SiteGroups.GetByID(int.Parse(aowner))); } else { web.AssociatedOwnerGroup = GetSiteGroup(web, web.Title + " Administrators", owner); roll.AddGroup(web.SiteGroups[web.Title + " Administrators"]); } } catch (Exception ex) { myLog.WriteEntry("Error Setting up Owner Group: " + ex.Message + ex.StackTrace, EventLogEntryType.Error, 334); } //=========Member Group======================== try { if (amember != "") { web.AssociatedMemberGroup = web.SiteGroups.GetByID(int.Parse(amember)); roll = web.Roles["Contribute"]; roll.AddGroup(web.SiteGroups.GetByID(int.Parse(amember))); } else { web.AssociatedMemberGroup = GetSiteGroup(web, web.Title + " Members", owner); roll = web.Roles["Contribute"]; roll.AddGroup(web.SiteGroups[web.Title + " Members"]); } } catch (Exception ex) { myLog.WriteEntry("Error Setting up Member Group: " + ex.Message + ex.StackTrace, EventLogEntryType.Error, 334); } //=========Visitor Group======================== try { if (avisitor != "") { web.AssociatedVisitorGroup = web.SiteGroups.GetByID(int.Parse(avisitor)); roll = web.Roles["Read"]; roll.AddGroup(web.SiteGroups.GetByID(int.Parse(avisitor))); } else { web.AssociatedVisitorGroup = GetSiteGroup(web, web.Title + " Visitors", owner); roll = web.Roles["Read"]; roll.AddGroup(web.SiteGroups[web.Title + " Visitors"]); } } catch (Exception ex) { myLog.WriteEntry("Error Setting up Visitor Group: " + ex.Message + ex.StackTrace, EventLogEntryType.Error, 334); } web.Update(); if (strEPMLiveGroupsPermAssignments.Length > 1) { string[] strOuter = strEPMLiveGroupsPermAssignments.Split(new string[] { "|~|" }, StringSplitOptions.None); foreach (string strInner in strOuter) { string[] strInnerMost = strInner.Split('~'); SPGroup spGroup = web.SiteGroups.GetByID(Convert.ToInt32(strInnerMost[0])); //Persist groups & permissions to the database if (spGroup != null) { SPRoleAssignment spRA = new SPRoleAssignment(spGroup); spRA.RoleDefinitionBindings.Add(web.RoleDefinitions.GetById(Convert.ToInt32(strInnerMost[1]))); web.RoleAssignments.Add(spRA); } } } web.Update(); } } }); }
private string createSite(string url, SPSiteSubscription subscription, string title, string description, string user, string fullName, string email, string template, out Guid siteid, bool useHostHeader) { siteid = Guid.Empty; string errors = ""; try { SPSite site; if (subscription != null) { site = app.Sites.Add(subscription, url, title, description, 1033, "", user, fullName, email, null, null, null, useHostHeader); } else { site = app.Sites.Add(url, title, description, 1033, "", user, fullName, email, null, null, null, useHostHeader); } try { //SPSite site = spApp.Sites.Add(bUrl + url, user, email); site.AllowUnsafeUpdates = true; using (SPWeb web = site.OpenWeb()) { web.Title = title; web.AllowUnsafeUpdates = true; web.Site.AllowUnsafeUpdates = true; web.Site.RootWeb.AllowUnsafeUpdates = true; //SPDocumentLibrary solGallery1 = (SPDocumentLibrary)web.Site.RootWeb.Site.GetCatalog(SPListTemplateType.SolutionCatalog); string[] files = Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.CommonProgramFiles) + "\\workengine\\templates\\" + template); int counter = 1; foreach (string file in files) { addfile(Path.GetFileNameWithoutExtension(file), web, file, counter); counter++; } SPWebTemplate webtemplate = null; foreach (SPWebTemplate t in web.GetAvailableWebTemplates((uint)web.Locale.LCID)) { if (t.Title == template) { webtemplate = t; break; } } if (webtemplate != null) { web.ApplyWebTemplate(webtemplate); } //web.Update(); //web.AllUsers.Add(user, email, fullName, ""); ////web.AllUsers.Add(System.Configuration.ConfigurationManager.AppSettings["owner"].ToString(), "", System.Configuration.ConfigurationManager.AppSettings["owner"].ToString(), ""); //web.Users[user].Name = fullName; //web.Users[user].Update(); //web.Update(); SPUser owner = web.AllUsers[user]; web.SiteGroups.Add("Administrators", owner, owner, ""); //web.Update(); web.AssociatedOwnerGroup = GetSiteGroup(web, "Administrators"); SPRole roll = web.Roles["Full Control"]; roll.AddGroup(web.SiteGroups["Administrators"]); SPMember newOwner = web.SiteGroups["Administrators"]; web.SiteGroups.Add("Team Members", newOwner, owner, ""); web.SiteGroups.Add("Visitors", newOwner, owner, ""); web.SiteGroups.Add("Project Managers", newOwner, owner, ""); //web.Update(); web.AssociatedVisitorGroup = GetSiteGroup(web, "Visitors"); web.AssociatedOwnerGroup = GetSiteGroup(web, "Administrators"); web.AssociatedMemberGroup = GetSiteGroup(web, "Team Members"); // web.Update(); //web.SiteGroups["Administrators"].Users[user].Name = fullName; //web.SiteGroups["Project Managers"].Users[user].Name = fullName; //web.SiteGroups["Team Members"].Users[user].Name = fullName; //web.SiteGroups["Visitors"].Users[user].Name = fullName; web.Roles.Add("Contribute2", "Can view, add, update, delete and manage subwebs", web.Roles["Contribute"].PermissionMask | SPRights.ManageSubwebs); roll = web.Roles["Full Control"]; roll.AddGroup(web.SiteGroups["Administrators"]); roll = web.Roles["Contribute"]; roll.AddGroup(web.SiteGroups["Team Members"]); roll = web.Roles["Read"]; roll.AddGroup(web.SiteGroups["Visitors"]); roll = web.Roles["Contribute2"]; roll.AddGroup(web.SiteGroups["Project Managers"]); siteid = site.ID; if (txtDatabaseServer.Text != "") { errors = mapReports(site); } } } catch (Exception ex) { errors = ex.ToString(); } finally { if (site != null) { site.Dispose(); } } } catch (Exception ex) { errors = ex.Message; } return(errors); }