protected override void CreateChildControls() { if (Field == null) { return; } base.CreateChildControls(); if (ControlMode == SPControlMode.Display) { return; } DataTable dt = new DataTable(); dt.Columns.Add("groupname"); dt.Columns.Add("groupid"); SPWeb web = SPContext.Current.Web; SPSecurity.RunWithElevatedPrivileges(delegate() { using (SPSite eSite = new SPSite(web.Site.ID)) { using (SPWeb eWeb = eSite.OpenWeb(web.ID)) { foreach (SPGroup group in APITeam.GetWebGroups(eWeb)) { dt.Rows.Add(new string[] { group.Name, group.ID.ToString() }); } } } }); ArrayList currentValues = new ArrayList(); try { SPFieldUserValue uv = new SPFieldUserValue(this.Web, this.ListItem["SharePointAccount"].ToString()); foreach (SPGroup g in uv.User.Groups) { currentValues.Add(g.ID.ToString()); } } catch { } chkPerms = (CheckBoxList)this.TemplateContainer.FindControl("chkPerms"); chkPerms.DataTextField = "groupname"; chkPerms.DataValueField = "groupid"; chkPerms.DataSource = dt; chkPerms.DataBind(); foreach (ListItem i in chkPerms.Items) { if (currentValues.Contains(i.Value.ToString())) { i.Selected = true; } } if (ControlMode == SPControlMode.New) { foreach (ListItem i in chkPerms.Items) { if (i.Text == "Team Members") { i.Selected = true; } } } }
private bool setPermissions(SPItemEventProperties properties, bool isAdd) { if (!properties.List.Fields.ContainsFieldWithInternalName("Permissions")) { return(false); } string curProps = ""; string newProps = ""; bool sendEmail = false; try { SPFieldUserValue uv = null; SPFieldUserValue uv_new = null; if (isAdd) { uv = new SPFieldUserValue(properties.Web, properties.AfterProperties["SharePointAccount"].ToString()); } else { uv = new SPFieldUserValue(properties.Web, properties.ListItem["SharePointAccount"].ToString()); if (properties.AfterProperties["SharePointAccount"] != null) { uv_new = new SPFieldUserValue(properties.Web, properties.AfterProperties["SharePointAccount"].ToString()); } } SPUser u = uv.User; if (u == null) { u = uv.User; if (u == null) { u = properties.Web.EnsureUser(uv.LookupValue); } } SPUser u_new = null; if (uv_new != null) { u_new = uv_new.User; if (u_new == null) { u_new = properties.Web.EnsureUser(uv_new.LookupValue); } } // semicolon causes issues in list cleanup and potentially in other modules. if ((isAdd && u.Name.Contains(";")) || (!isAdd && u_new != null && u_new.Name.Contains(";"))) { throw new InvalidOperationException("Semicolon is not allowed for AD username."); } try { curProps = properties.ListItem["Permissions"].ToString().Trim(); } catch { } try { newProps = properties.AfterProperties["Permissions"].ToString(); } catch { return(false); } ArrayList arr = new ArrayList(newProps.Split(',')); var webGroups = APITeam.GetWebGroups(properties.Web); foreach (SPGroup group in webGroups) { if (arr.Contains(group.ID.ToString())) { try { group.AddUser(u); } catch { } } else { try { group.RemoveUser(u); } catch { } } } if (!String.IsNullOrEmpty(newProps) && String.IsNullOrEmpty(curProps)) { sendEmail = true; } try { string perms = ""; foreach (SPGroup wGroup in webGroups) { try { SPGroup g = u.Groups.GetByID(wGroup.ID); if (g != null && arr.Contains(g.ID.ToString(CultureInfo.InvariantCulture))) { perms += ", " + wGroup.Name; } } catch { } } properties.AfterProperties["Permissions"] = perms.Trim(", ".ToCharArray()); } catch { } } catch (InvalidOperationException ex) { throw ex; } catch { } return(sendEmail); }